cgs_common.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * Copyright 2015 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. *
  23. */
  24. #ifndef _CGS_COMMON_H
  25. #define _CGS_COMMON_H
  26. #include "amd_shared.h"
  27. struct cgs_device;
  28. /**
  29. * enum cgs_gpu_mem_type - GPU memory types
  30. */
  31. enum cgs_gpu_mem_type {
  32. CGS_GPU_MEM_TYPE__VISIBLE_FB,
  33. CGS_GPU_MEM_TYPE__INVISIBLE_FB,
  34. CGS_GPU_MEM_TYPE__VISIBLE_CONTIG_FB,
  35. CGS_GPU_MEM_TYPE__INVISIBLE_CONTIG_FB,
  36. CGS_GPU_MEM_TYPE__GART_CACHEABLE,
  37. CGS_GPU_MEM_TYPE__GART_WRITECOMBINE
  38. };
  39. /**
  40. * enum cgs_ind_reg - Indirect register spaces
  41. */
  42. enum cgs_ind_reg {
  43. CGS_IND_REG__MMIO,
  44. CGS_IND_REG__PCIE,
  45. CGS_IND_REG__SMC,
  46. CGS_IND_REG__UVD_CTX,
  47. CGS_IND_REG__DIDT,
  48. CGS_IND_REG_GC_CAC,
  49. CGS_IND_REG__AUDIO_ENDPT
  50. };
  51. /**
  52. * enum cgs_engine - Engines that can be statically power-gated
  53. */
  54. enum cgs_engine {
  55. CGS_ENGINE__UVD,
  56. CGS_ENGINE__VCE,
  57. CGS_ENGINE__VP8,
  58. CGS_ENGINE__ACP_DMA,
  59. CGS_ENGINE__ACP_DSP0,
  60. CGS_ENGINE__ACP_DSP1,
  61. CGS_ENGINE__ISP,
  62. /* ... */
  63. };
  64. /*
  65. * enum cgs_ucode_id - Firmware types for different IPs
  66. */
  67. enum cgs_ucode_id {
  68. CGS_UCODE_ID_SMU = 0,
  69. CGS_UCODE_ID_SMU_SK,
  70. CGS_UCODE_ID_SDMA0,
  71. CGS_UCODE_ID_SDMA1,
  72. CGS_UCODE_ID_CP_CE,
  73. CGS_UCODE_ID_CP_PFP,
  74. CGS_UCODE_ID_CP_ME,
  75. CGS_UCODE_ID_CP_MEC,
  76. CGS_UCODE_ID_CP_MEC_JT1,
  77. CGS_UCODE_ID_CP_MEC_JT2,
  78. CGS_UCODE_ID_GMCON_RENG,
  79. CGS_UCODE_ID_RLC_G,
  80. CGS_UCODE_ID_STORAGE,
  81. CGS_UCODE_ID_MAXIMUM,
  82. };
  83. enum cgs_system_info_id {
  84. CGS_SYSTEM_INFO_ADAPTER_BDF_ID = 1,
  85. CGS_SYSTEM_INFO_PCIE_GEN_INFO,
  86. CGS_SYSTEM_INFO_PCIE_MLW,
  87. CGS_SYSTEM_INFO_PCIE_DEV,
  88. CGS_SYSTEM_INFO_PCIE_REV,
  89. CGS_SYSTEM_INFO_CG_FLAGS,
  90. CGS_SYSTEM_INFO_PG_FLAGS,
  91. CGS_SYSTEM_INFO_GFX_CU_INFO,
  92. CGS_SYSTEM_INFO_GFX_SE_INFO,
  93. CGS_SYSTEM_INFO_PCIE_SUB_SYS_ID,
  94. CGS_SYSTEM_INFO_PCIE_SUB_SYS_VENDOR_ID,
  95. CGS_SYSTEM_INFO_ID_MAXIMUM,
  96. };
  97. struct cgs_system_info {
  98. uint64_t size;
  99. enum cgs_system_info_id info_id;
  100. union {
  101. void *ptr;
  102. uint64_t value;
  103. };
  104. uint64_t padding[13];
  105. };
  106. /*
  107. * enum cgs_resource_type - GPU resource type
  108. */
  109. enum cgs_resource_type {
  110. CGS_RESOURCE_TYPE_MMIO = 0,
  111. CGS_RESOURCE_TYPE_FB,
  112. CGS_RESOURCE_TYPE_IO,
  113. CGS_RESOURCE_TYPE_DOORBELL,
  114. CGS_RESOURCE_TYPE_ROM,
  115. };
  116. /**
  117. * struct cgs_firmware_info - Firmware information
  118. */
  119. struct cgs_firmware_info {
  120. uint16_t version;
  121. uint16_t fw_version;
  122. uint16_t feature_version;
  123. uint32_t image_size;
  124. uint64_t mc_addr;
  125. /* only for smc firmware */
  126. uint32_t ucode_start_address;
  127. void *kptr;
  128. bool is_kicker;
  129. };
  130. struct cgs_mode_info {
  131. uint32_t refresh_rate;
  132. uint32_t ref_clock;
  133. uint32_t vblank_time_us;
  134. };
  135. struct cgs_display_info {
  136. uint32_t display_count;
  137. uint32_t active_display_mask;
  138. struct cgs_mode_info *mode_info;
  139. };
  140. typedef unsigned long cgs_handle_t;
  141. #define CGS_ACPI_METHOD_ATCS 0x53435441
  142. #define CGS_ACPI_METHOD_ATIF 0x46495441
  143. #define CGS_ACPI_METHOD_ATPX 0x58505441
  144. #define CGS_ACPI_FIELD_METHOD_NAME 0x00000001
  145. #define CGS_ACPI_FIELD_INPUT_ARGUMENT_COUNT 0x00000002
  146. #define CGS_ACPI_MAX_BUFFER_SIZE 256
  147. #define CGS_ACPI_TYPE_ANY 0x00
  148. #define CGS_ACPI_TYPE_INTEGER 0x01
  149. #define CGS_ACPI_TYPE_STRING 0x02
  150. #define CGS_ACPI_TYPE_BUFFER 0x03
  151. #define CGS_ACPI_TYPE_PACKAGE 0x04
  152. struct cgs_acpi_method_argument {
  153. uint32_t type;
  154. uint32_t data_length;
  155. union{
  156. uint32_t value;
  157. void *pointer;
  158. };
  159. };
  160. struct cgs_acpi_method_info {
  161. uint32_t size;
  162. uint32_t field;
  163. uint32_t input_count;
  164. uint32_t name;
  165. struct cgs_acpi_method_argument *pinput_argument;
  166. uint32_t output_count;
  167. struct cgs_acpi_method_argument *poutput_argument;
  168. uint32_t padding[9];
  169. };
  170. /**
  171. * cgs_alloc_gpu_mem() - Allocate GPU memory
  172. * @cgs_device: opaque device handle
  173. * @type: memory type
  174. * @size: size in bytes
  175. * @align: alignment in bytes
  176. * @min_offset: minimum offset from start of heap
  177. * @max_offset: maximum offset from start of heap
  178. * @handle: memory handle (output)
  179. *
  180. * The memory types CGS_GPU_MEM_TYPE_*_CONTIG_FB force contiguous
  181. * memory allocation. This guarantees that the MC address returned by
  182. * cgs_gmap_gpu_mem is not mapped through the GART. The non-contiguous
  183. * FB memory types may be GART mapped depending on memory
  184. * fragmentation and memory allocator policies.
  185. *
  186. * If min/max_offset are non-0, the allocation will be forced to
  187. * reside between these offsets in its respective memory heap. The
  188. * base address that the offset relates to, depends on the memory
  189. * type.
  190. *
  191. * - CGS_GPU_MEM_TYPE__*_CONTIG_FB: FB MC base address
  192. * - CGS_GPU_MEM_TYPE__GART_*: GART aperture base address
  193. * - others: undefined, don't use with max_offset
  194. *
  195. * Return: 0 on success, -errno otherwise
  196. */
  197. typedef int (*cgs_alloc_gpu_mem_t)(struct cgs_device *cgs_device, enum cgs_gpu_mem_type type,
  198. uint64_t size, uint64_t align,
  199. uint64_t min_offset, uint64_t max_offset,
  200. cgs_handle_t *handle);
  201. /**
  202. * cgs_free_gpu_mem() - Free GPU memory
  203. * @cgs_device: opaque device handle
  204. * @handle: memory handle returned by alloc or import
  205. *
  206. * Return: 0 on success, -errno otherwise
  207. */
  208. typedef int (*cgs_free_gpu_mem_t)(struct cgs_device *cgs_device, cgs_handle_t handle);
  209. /**
  210. * cgs_gmap_gpu_mem() - GPU-map GPU memory
  211. * @cgs_device: opaque device handle
  212. * @handle: memory handle returned by alloc or import
  213. * @mcaddr: MC address (output)
  214. *
  215. * Ensures that a buffer is GPU accessible and returns its MC address.
  216. *
  217. * Return: 0 on success, -errno otherwise
  218. */
  219. typedef int (*cgs_gmap_gpu_mem_t)(struct cgs_device *cgs_device, cgs_handle_t handle,
  220. uint64_t *mcaddr);
  221. /**
  222. * cgs_gunmap_gpu_mem() - GPU-unmap GPU memory
  223. * @cgs_device: opaque device handle
  224. * @handle: memory handle returned by alloc or import
  225. *
  226. * Allows the buffer to be migrated while it's not used by the GPU.
  227. *
  228. * Return: 0 on success, -errno otherwise
  229. */
  230. typedef int (*cgs_gunmap_gpu_mem_t)(struct cgs_device *cgs_device, cgs_handle_t handle);
  231. /**
  232. * cgs_kmap_gpu_mem() - Kernel-map GPU memory
  233. *
  234. * @cgs_device: opaque device handle
  235. * @handle: memory handle returned by alloc or import
  236. * @map: Kernel virtual address the memory was mapped to (output)
  237. *
  238. * Return: 0 on success, -errno otherwise
  239. */
  240. typedef int (*cgs_kmap_gpu_mem_t)(struct cgs_device *cgs_device, cgs_handle_t handle,
  241. void **map);
  242. /**
  243. * cgs_kunmap_gpu_mem() - Kernel-unmap GPU memory
  244. * @cgs_device: opaque device handle
  245. * @handle: memory handle returned by alloc or import
  246. *
  247. * Return: 0 on success, -errno otherwise
  248. */
  249. typedef int (*cgs_kunmap_gpu_mem_t)(struct cgs_device *cgs_device, cgs_handle_t handle);
  250. /**
  251. * cgs_read_register() - Read an MMIO register
  252. * @cgs_device: opaque device handle
  253. * @offset: register offset
  254. *
  255. * Return: register value
  256. */
  257. typedef uint32_t (*cgs_read_register_t)(struct cgs_device *cgs_device, unsigned offset);
  258. /**
  259. * cgs_write_register() - Write an MMIO register
  260. * @cgs_device: opaque device handle
  261. * @offset: register offset
  262. * @value: register value
  263. */
  264. typedef void (*cgs_write_register_t)(struct cgs_device *cgs_device, unsigned offset,
  265. uint32_t value);
  266. /**
  267. * cgs_read_ind_register() - Read an indirect register
  268. * @cgs_device: opaque device handle
  269. * @offset: register offset
  270. *
  271. * Return: register value
  272. */
  273. typedef uint32_t (*cgs_read_ind_register_t)(struct cgs_device *cgs_device, enum cgs_ind_reg space,
  274. unsigned index);
  275. /**
  276. * cgs_write_ind_register() - Write an indirect register
  277. * @cgs_device: opaque device handle
  278. * @offset: register offset
  279. * @value: register value
  280. */
  281. typedef void (*cgs_write_ind_register_t)(struct cgs_device *cgs_device, enum cgs_ind_reg space,
  282. unsigned index, uint32_t value);
  283. /**
  284. * cgs_get_pci_resource() - provide access to a device resource (PCI BAR)
  285. * @cgs_device: opaque device handle
  286. * @resource_type: Type of Resource (MMIO, IO, ROM, FB, DOORBELL)
  287. * @size: size of the region
  288. * @offset: offset from the start of the region
  289. * @resource_base: base address (not including offset) returned
  290. *
  291. * Return: 0 on success, -errno otherwise
  292. */
  293. typedef int (*cgs_get_pci_resource_t)(struct cgs_device *cgs_device,
  294. enum cgs_resource_type resource_type,
  295. uint64_t size,
  296. uint64_t offset,
  297. uint64_t *resource_base);
  298. /**
  299. * cgs_atom_get_data_table() - Get a pointer to an ATOM BIOS data table
  300. * @cgs_device: opaque device handle
  301. * @table: data table index
  302. * @size: size of the table (output, may be NULL)
  303. * @frev: table format revision (output, may be NULL)
  304. * @crev: table content revision (output, may be NULL)
  305. *
  306. * Return: Pointer to start of the table, or NULL on failure
  307. */
  308. typedef const void *(*cgs_atom_get_data_table_t)(
  309. struct cgs_device *cgs_device, unsigned table,
  310. uint16_t *size, uint8_t *frev, uint8_t *crev);
  311. /**
  312. * cgs_atom_get_cmd_table_revs() - Get ATOM BIOS command table revisions
  313. * @cgs_device: opaque device handle
  314. * @table: data table index
  315. * @frev: table format revision (output, may be NULL)
  316. * @crev: table content revision (output, may be NULL)
  317. *
  318. * Return: 0 on success, -errno otherwise
  319. */
  320. typedef int (*cgs_atom_get_cmd_table_revs_t)(struct cgs_device *cgs_device, unsigned table,
  321. uint8_t *frev, uint8_t *crev);
  322. /**
  323. * cgs_atom_exec_cmd_table() - Execute an ATOM BIOS command table
  324. * @cgs_device: opaque device handle
  325. * @table: command table index
  326. * @args: arguments
  327. *
  328. * Return: 0 on success, -errno otherwise
  329. */
  330. typedef int (*cgs_atom_exec_cmd_table_t)(struct cgs_device *cgs_device,
  331. unsigned table, void *args);
  332. /**
  333. * cgs_get_firmware_info - Get the firmware information from core driver
  334. * @cgs_device: opaque device handle
  335. * @type: the firmware type
  336. * @info: returend firmware information
  337. *
  338. * Return: 0 on success, -errno otherwise
  339. */
  340. typedef int (*cgs_get_firmware_info)(struct cgs_device *cgs_device,
  341. enum cgs_ucode_id type,
  342. struct cgs_firmware_info *info);
  343. typedef int (*cgs_rel_firmware)(struct cgs_device *cgs_device,
  344. enum cgs_ucode_id type);
  345. typedef int(*cgs_set_powergating_state)(struct cgs_device *cgs_device,
  346. enum amd_ip_block_type block_type,
  347. enum amd_powergating_state state);
  348. typedef int(*cgs_set_clockgating_state)(struct cgs_device *cgs_device,
  349. enum amd_ip_block_type block_type,
  350. enum amd_clockgating_state state);
  351. typedef int(*cgs_get_active_displays_info)(
  352. struct cgs_device *cgs_device,
  353. struct cgs_display_info *info);
  354. typedef int (*cgs_notify_dpm_enabled)(struct cgs_device *cgs_device, bool enabled);
  355. typedef int (*cgs_call_acpi_method)(struct cgs_device *cgs_device,
  356. uint32_t acpi_method,
  357. uint32_t acpi_function,
  358. void *pinput, void *poutput,
  359. uint32_t output_count,
  360. uint32_t input_size,
  361. uint32_t output_size);
  362. typedef int (*cgs_query_system_info)(struct cgs_device *cgs_device,
  363. struct cgs_system_info *sys_info);
  364. typedef int (*cgs_is_virtualization_enabled_t)(void *cgs_device);
  365. typedef int (*cgs_enter_safe_mode)(struct cgs_device *cgs_device, bool en);
  366. struct cgs_ops {
  367. /* memory management calls (similar to KFD interface) */
  368. cgs_alloc_gpu_mem_t alloc_gpu_mem;
  369. cgs_free_gpu_mem_t free_gpu_mem;
  370. cgs_gmap_gpu_mem_t gmap_gpu_mem;
  371. cgs_gunmap_gpu_mem_t gunmap_gpu_mem;
  372. cgs_kmap_gpu_mem_t kmap_gpu_mem;
  373. cgs_kunmap_gpu_mem_t kunmap_gpu_mem;
  374. /* MMIO access */
  375. cgs_read_register_t read_register;
  376. cgs_write_register_t write_register;
  377. cgs_read_ind_register_t read_ind_register;
  378. cgs_write_ind_register_t write_ind_register;
  379. /* PCI resources */
  380. cgs_get_pci_resource_t get_pci_resource;
  381. /* ATOM BIOS */
  382. cgs_atom_get_data_table_t atom_get_data_table;
  383. cgs_atom_get_cmd_table_revs_t atom_get_cmd_table_revs;
  384. cgs_atom_exec_cmd_table_t atom_exec_cmd_table;
  385. /* Firmware Info */
  386. cgs_get_firmware_info get_firmware_info;
  387. cgs_rel_firmware rel_firmware;
  388. /* cg pg interface*/
  389. cgs_set_powergating_state set_powergating_state;
  390. cgs_set_clockgating_state set_clockgating_state;
  391. /* display manager */
  392. cgs_get_active_displays_info get_active_displays_info;
  393. /* notify dpm enabled */
  394. cgs_notify_dpm_enabled notify_dpm_enabled;
  395. /* ACPI */
  396. cgs_call_acpi_method call_acpi_method;
  397. /* get system info */
  398. cgs_query_system_info query_system_info;
  399. cgs_is_virtualization_enabled_t is_virtualization_enabled;
  400. cgs_enter_safe_mode enter_safe_mode;
  401. };
  402. struct cgs_os_ops; /* To be define in OS-specific CGS header */
  403. struct cgs_device
  404. {
  405. const struct cgs_ops *ops;
  406. const struct cgs_os_ops *os_ops;
  407. /* to be embedded at the start of driver private structure */
  408. };
  409. /* Convenience macros that make CGS indirect function calls look like
  410. * normal function calls */
  411. #define CGS_CALL(func,dev,...) \
  412. (((struct cgs_device *)dev)->ops->func(dev, ##__VA_ARGS__))
  413. #define CGS_OS_CALL(func,dev,...) \
  414. (((struct cgs_device *)dev)->os_ops->func(dev, ##__VA_ARGS__))
  415. #define cgs_alloc_gpu_mem(dev,type,size,align,min_off,max_off,handle) \
  416. CGS_CALL(alloc_gpu_mem,dev,type,size,align,min_off,max_off,handle)
  417. #define cgs_free_gpu_mem(dev,handle) \
  418. CGS_CALL(free_gpu_mem,dev,handle)
  419. #define cgs_gmap_gpu_mem(dev,handle,mcaddr) \
  420. CGS_CALL(gmap_gpu_mem,dev,handle,mcaddr)
  421. #define cgs_gunmap_gpu_mem(dev,handle) \
  422. CGS_CALL(gunmap_gpu_mem,dev,handle)
  423. #define cgs_kmap_gpu_mem(dev,handle,map) \
  424. CGS_CALL(kmap_gpu_mem,dev,handle,map)
  425. #define cgs_kunmap_gpu_mem(dev,handle) \
  426. CGS_CALL(kunmap_gpu_mem,dev,handle)
  427. #define cgs_read_register(dev,offset) \
  428. CGS_CALL(read_register,dev,offset)
  429. #define cgs_write_register(dev,offset,value) \
  430. CGS_CALL(write_register,dev,offset,value)
  431. #define cgs_read_ind_register(dev,space,index) \
  432. CGS_CALL(read_ind_register,dev,space,index)
  433. #define cgs_write_ind_register(dev,space,index,value) \
  434. CGS_CALL(write_ind_register,dev,space,index,value)
  435. #define cgs_atom_get_data_table(dev,table,size,frev,crev) \
  436. CGS_CALL(atom_get_data_table,dev,table,size,frev,crev)
  437. #define cgs_atom_get_cmd_table_revs(dev,table,frev,crev) \
  438. CGS_CALL(atom_get_cmd_table_revs,dev,table,frev,crev)
  439. #define cgs_atom_exec_cmd_table(dev,table,args) \
  440. CGS_CALL(atom_exec_cmd_table,dev,table,args)
  441. #define cgs_get_firmware_info(dev, type, info) \
  442. CGS_CALL(get_firmware_info, dev, type, info)
  443. #define cgs_rel_firmware(dev, type) \
  444. CGS_CALL(rel_firmware, dev, type)
  445. #define cgs_set_powergating_state(dev, block_type, state) \
  446. CGS_CALL(set_powergating_state, dev, block_type, state)
  447. #define cgs_set_clockgating_state(dev, block_type, state) \
  448. CGS_CALL(set_clockgating_state, dev, block_type, state)
  449. #define cgs_notify_dpm_enabled(dev, enabled) \
  450. CGS_CALL(notify_dpm_enabled, dev, enabled)
  451. #define cgs_get_active_displays_info(dev, info) \
  452. CGS_CALL(get_active_displays_info, dev, info)
  453. #define cgs_call_acpi_method(dev, acpi_method, acpi_function, pintput, poutput, output_count, input_size, output_size) \
  454. CGS_CALL(call_acpi_method, dev, acpi_method, acpi_function, pintput, poutput, output_count, input_size, output_size)
  455. #define cgs_query_system_info(dev, sys_info) \
  456. CGS_CALL(query_system_info, dev, sys_info)
  457. #define cgs_get_pci_resource(cgs_device, resource_type, size, offset, \
  458. resource_base) \
  459. CGS_CALL(get_pci_resource, cgs_device, resource_type, size, offset, \
  460. resource_base)
  461. #define cgs_is_virtualization_enabled(cgs_device) \
  462. CGS_CALL(is_virtualization_enabled, cgs_device)
  463. #define cgs_enter_safe_mode(cgs_device, en) \
  464. CGS_CALL(enter_safe_mode, cgs_device, en)
  465. #endif /* _CGS_COMMON_H */