cgs_common.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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_SE_CAC,
  50. CGS_IND_REG__AUDIO_ENDPT
  51. };
  52. /**
  53. * enum cgs_engine - Engines that can be statically power-gated
  54. */
  55. enum cgs_engine {
  56. CGS_ENGINE__UVD,
  57. CGS_ENGINE__VCE,
  58. CGS_ENGINE__VP8,
  59. CGS_ENGINE__ACP_DMA,
  60. CGS_ENGINE__ACP_DSP0,
  61. CGS_ENGINE__ACP_DSP1,
  62. CGS_ENGINE__ISP,
  63. /* ... */
  64. };
  65. /*
  66. * enum cgs_ucode_id - Firmware types for different IPs
  67. */
  68. enum cgs_ucode_id {
  69. CGS_UCODE_ID_SMU = 0,
  70. CGS_UCODE_ID_SMU_SK,
  71. CGS_UCODE_ID_SDMA0,
  72. CGS_UCODE_ID_SDMA1,
  73. CGS_UCODE_ID_CP_CE,
  74. CGS_UCODE_ID_CP_PFP,
  75. CGS_UCODE_ID_CP_ME,
  76. CGS_UCODE_ID_CP_MEC,
  77. CGS_UCODE_ID_CP_MEC_JT1,
  78. CGS_UCODE_ID_CP_MEC_JT2,
  79. CGS_UCODE_ID_GMCON_RENG,
  80. CGS_UCODE_ID_RLC_G,
  81. CGS_UCODE_ID_STORAGE,
  82. CGS_UCODE_ID_MAXIMUM,
  83. };
  84. enum cgs_system_info_id {
  85. CGS_SYSTEM_INFO_ADAPTER_BDF_ID = 1,
  86. CGS_SYSTEM_INFO_PCIE_GEN_INFO,
  87. CGS_SYSTEM_INFO_PCIE_MLW,
  88. CGS_SYSTEM_INFO_PCIE_DEV,
  89. CGS_SYSTEM_INFO_PCIE_REV,
  90. CGS_SYSTEM_INFO_CG_FLAGS,
  91. CGS_SYSTEM_INFO_PG_FLAGS,
  92. CGS_SYSTEM_INFO_GFX_CU_INFO,
  93. CGS_SYSTEM_INFO_GFX_SE_INFO,
  94. CGS_SYSTEM_INFO_PCIE_SUB_SYS_ID,
  95. CGS_SYSTEM_INFO_PCIE_SUB_SYS_VENDOR_ID,
  96. CGS_SYSTEM_INFO_PCIE_BUS_DEVFN,
  97. CGS_SYSTEM_INFO_VRAM_WIDTH,
  98. CGS_SYSTEM_INFO_ID_MAXIMUM,
  99. };
  100. struct cgs_system_info {
  101. uint64_t size;
  102. enum cgs_system_info_id info_id;
  103. union {
  104. void *ptr;
  105. uint64_t value;
  106. };
  107. uint64_t padding[13];
  108. };
  109. /*
  110. * enum cgs_resource_type - GPU resource type
  111. */
  112. enum cgs_resource_type {
  113. CGS_RESOURCE_TYPE_MMIO = 0,
  114. CGS_RESOURCE_TYPE_FB,
  115. CGS_RESOURCE_TYPE_IO,
  116. CGS_RESOURCE_TYPE_DOORBELL,
  117. CGS_RESOURCE_TYPE_ROM,
  118. };
  119. /**
  120. * struct cgs_firmware_info - Firmware information
  121. */
  122. struct cgs_firmware_info {
  123. uint16_t version;
  124. uint16_t fw_version;
  125. uint16_t feature_version;
  126. uint32_t image_size;
  127. uint64_t mc_addr;
  128. /* only for smc firmware */
  129. uint32_t ucode_start_address;
  130. void *kptr;
  131. bool is_kicker;
  132. };
  133. struct cgs_mode_info {
  134. uint32_t refresh_rate;
  135. uint32_t ref_clock;
  136. uint32_t vblank_time_us;
  137. };
  138. struct cgs_display_info {
  139. uint32_t display_count;
  140. uint32_t active_display_mask;
  141. struct cgs_mode_info *mode_info;
  142. };
  143. typedef unsigned long cgs_handle_t;
  144. /**
  145. * cgs_alloc_gpu_mem() - Allocate GPU memory
  146. * @cgs_device: opaque device handle
  147. * @type: memory type
  148. * @size: size in bytes
  149. * @align: alignment in bytes
  150. * @handle: memory handle (output)
  151. *
  152. * The memory types CGS_GPU_MEM_TYPE_*_CONTIG_FB force contiguous
  153. * memory allocation. This guarantees that the MC address returned by
  154. * cgs_gmap_gpu_mem is not mapped through the GART. The non-contiguous
  155. * FB memory types may be GART mapped depending on memory
  156. * fragmentation and memory allocator policies.
  157. *
  158. * If min/max_offset are non-0, the allocation will be forced to
  159. * reside between these offsets in its respective memory heap. The
  160. * base address that the offset relates to, depends on the memory
  161. * type.
  162. *
  163. * - CGS_GPU_MEM_TYPE__*_CONTIG_FB: FB MC base address
  164. * - CGS_GPU_MEM_TYPE__GART_*: GART aperture base address
  165. * - others: undefined, don't use with max_offset
  166. *
  167. * Return: 0 on success, -errno otherwise
  168. */
  169. typedef int (*cgs_alloc_gpu_mem_t)(struct cgs_device *cgs_device, enum cgs_gpu_mem_type type,
  170. uint64_t size, uint64_t align,
  171. cgs_handle_t *handle);
  172. /**
  173. * cgs_free_gpu_mem() - Free GPU memory
  174. * @cgs_device: opaque device handle
  175. * @handle: memory handle returned by alloc or import
  176. *
  177. * Return: 0 on success, -errno otherwise
  178. */
  179. typedef int (*cgs_free_gpu_mem_t)(struct cgs_device *cgs_device, cgs_handle_t handle);
  180. /**
  181. * cgs_gmap_gpu_mem() - GPU-map GPU memory
  182. * @cgs_device: opaque device handle
  183. * @handle: memory handle returned by alloc or import
  184. * @mcaddr: MC address (output)
  185. *
  186. * Ensures that a buffer is GPU accessible and returns its MC address.
  187. *
  188. * Return: 0 on success, -errno otherwise
  189. */
  190. typedef int (*cgs_gmap_gpu_mem_t)(struct cgs_device *cgs_device, cgs_handle_t handle,
  191. uint64_t *mcaddr);
  192. /**
  193. * cgs_gunmap_gpu_mem() - GPU-unmap GPU memory
  194. * @cgs_device: opaque device handle
  195. * @handle: memory handle returned by alloc or import
  196. *
  197. * Allows the buffer to be migrated while it's not used by the GPU.
  198. *
  199. * Return: 0 on success, -errno otherwise
  200. */
  201. typedef int (*cgs_gunmap_gpu_mem_t)(struct cgs_device *cgs_device, cgs_handle_t handle);
  202. /**
  203. * cgs_kmap_gpu_mem() - Kernel-map GPU memory
  204. *
  205. * @cgs_device: opaque device handle
  206. * @handle: memory handle returned by alloc or import
  207. * @map: Kernel virtual address the memory was mapped to (output)
  208. *
  209. * Return: 0 on success, -errno otherwise
  210. */
  211. typedef int (*cgs_kmap_gpu_mem_t)(struct cgs_device *cgs_device, cgs_handle_t handle,
  212. void **map);
  213. /**
  214. * cgs_kunmap_gpu_mem() - Kernel-unmap GPU memory
  215. * @cgs_device: opaque device handle
  216. * @handle: memory handle returned by alloc or import
  217. *
  218. * Return: 0 on success, -errno otherwise
  219. */
  220. typedef int (*cgs_kunmap_gpu_mem_t)(struct cgs_device *cgs_device, cgs_handle_t handle);
  221. /**
  222. * cgs_read_register() - Read an MMIO register
  223. * @cgs_device: opaque device handle
  224. * @offset: register offset
  225. *
  226. * Return: register value
  227. */
  228. typedef uint32_t (*cgs_read_register_t)(struct cgs_device *cgs_device, unsigned offset);
  229. /**
  230. * cgs_write_register() - Write an MMIO register
  231. * @cgs_device: opaque device handle
  232. * @offset: register offset
  233. * @value: register value
  234. */
  235. typedef void (*cgs_write_register_t)(struct cgs_device *cgs_device, unsigned offset,
  236. uint32_t value);
  237. /**
  238. * cgs_read_ind_register() - Read an indirect register
  239. * @cgs_device: opaque device handle
  240. * @offset: register offset
  241. *
  242. * Return: register value
  243. */
  244. typedef uint32_t (*cgs_read_ind_register_t)(struct cgs_device *cgs_device, enum cgs_ind_reg space,
  245. unsigned index);
  246. /**
  247. * cgs_write_ind_register() - Write an indirect register
  248. * @cgs_device: opaque device handle
  249. * @offset: register offset
  250. * @value: register value
  251. */
  252. typedef void (*cgs_write_ind_register_t)(struct cgs_device *cgs_device, enum cgs_ind_reg space,
  253. unsigned index, uint32_t value);
  254. #define CGS_REG_FIELD_SHIFT(reg, field) reg##__##field##__SHIFT
  255. #define CGS_REG_FIELD_MASK(reg, field) reg##__##field##_MASK
  256. #define CGS_REG_SET_FIELD(orig_val, reg, field, field_val) \
  257. (((orig_val) & ~CGS_REG_FIELD_MASK(reg, field)) | \
  258. (CGS_REG_FIELD_MASK(reg, field) & ((field_val) << CGS_REG_FIELD_SHIFT(reg, field))))
  259. #define CGS_REG_GET_FIELD(value, reg, field) \
  260. (((value) & CGS_REG_FIELD_MASK(reg, field)) >> CGS_REG_FIELD_SHIFT(reg, field))
  261. #define CGS_WREG32_FIELD(device, reg, field, val) \
  262. cgs_write_register(device, mm##reg, (cgs_read_register(device, mm##reg) & ~CGS_REG_FIELD_MASK(reg, field)) | (val) << CGS_REG_FIELD_SHIFT(reg, field))
  263. #define CGS_WREG32_FIELD_IND(device, space, reg, field, val) \
  264. cgs_write_ind_register(device, space, ix##reg, (cgs_read_ind_register(device, space, ix##reg) & ~CGS_REG_FIELD_MASK(reg, field)) | (val) << CGS_REG_FIELD_SHIFT(reg, field))
  265. /**
  266. * cgs_get_pci_resource() - provide access to a device resource (PCI BAR)
  267. * @cgs_device: opaque device handle
  268. * @resource_type: Type of Resource (MMIO, IO, ROM, FB, DOORBELL)
  269. * @size: size of the region
  270. * @offset: offset from the start of the region
  271. * @resource_base: base address (not including offset) returned
  272. *
  273. * Return: 0 on success, -errno otherwise
  274. */
  275. typedef int (*cgs_get_pci_resource_t)(struct cgs_device *cgs_device,
  276. enum cgs_resource_type resource_type,
  277. uint64_t size,
  278. uint64_t offset,
  279. uint64_t *resource_base);
  280. /**
  281. * cgs_atom_get_data_table() - Get a pointer to an ATOM BIOS data table
  282. * @cgs_device: opaque device handle
  283. * @table: data table index
  284. * @size: size of the table (output, may be NULL)
  285. * @frev: table format revision (output, may be NULL)
  286. * @crev: table content revision (output, may be NULL)
  287. *
  288. * Return: Pointer to start of the table, or NULL on failure
  289. */
  290. typedef const void *(*cgs_atom_get_data_table_t)(
  291. struct cgs_device *cgs_device, unsigned table,
  292. uint16_t *size, uint8_t *frev, uint8_t *crev);
  293. /**
  294. * cgs_atom_get_cmd_table_revs() - Get ATOM BIOS command table revisions
  295. * @cgs_device: opaque device handle
  296. * @table: data table index
  297. * @frev: table format revision (output, may be NULL)
  298. * @crev: table content revision (output, may be NULL)
  299. *
  300. * Return: 0 on success, -errno otherwise
  301. */
  302. typedef int (*cgs_atom_get_cmd_table_revs_t)(struct cgs_device *cgs_device, unsigned table,
  303. uint8_t *frev, uint8_t *crev);
  304. /**
  305. * cgs_atom_exec_cmd_table() - Execute an ATOM BIOS command table
  306. * @cgs_device: opaque device handle
  307. * @table: command table index
  308. * @args: arguments
  309. *
  310. * Return: 0 on success, -errno otherwise
  311. */
  312. typedef int (*cgs_atom_exec_cmd_table_t)(struct cgs_device *cgs_device,
  313. unsigned table, void *args);
  314. /**
  315. * cgs_get_firmware_info - Get the firmware information from core driver
  316. * @cgs_device: opaque device handle
  317. * @type: the firmware type
  318. * @info: returend firmware information
  319. *
  320. * Return: 0 on success, -errno otherwise
  321. */
  322. typedef int (*cgs_get_firmware_info)(struct cgs_device *cgs_device,
  323. enum cgs_ucode_id type,
  324. struct cgs_firmware_info *info);
  325. typedef int (*cgs_rel_firmware)(struct cgs_device *cgs_device,
  326. enum cgs_ucode_id type);
  327. typedef int(*cgs_set_powergating_state)(struct cgs_device *cgs_device,
  328. enum amd_ip_block_type block_type,
  329. enum amd_powergating_state state);
  330. typedef int(*cgs_set_clockgating_state)(struct cgs_device *cgs_device,
  331. enum amd_ip_block_type block_type,
  332. enum amd_clockgating_state state);
  333. typedef int(*cgs_get_active_displays_info)(
  334. struct cgs_device *cgs_device,
  335. struct cgs_display_info *info);
  336. typedef int (*cgs_notify_dpm_enabled)(struct cgs_device *cgs_device, bool enabled);
  337. typedef int (*cgs_query_system_info)(struct cgs_device *cgs_device,
  338. struct cgs_system_info *sys_info);
  339. typedef int (*cgs_is_virtualization_enabled_t)(void *cgs_device);
  340. typedef int (*cgs_enter_safe_mode)(struct cgs_device *cgs_device, bool en);
  341. typedef void (*cgs_lock_grbm_idx)(struct cgs_device *cgs_device, bool lock);
  342. typedef int (*cgs_set_temperature_range)(struct cgs_device *cgs_device,
  343. int min_temperature,
  344. int max_temperature);
  345. struct cgs_ops {
  346. /* memory management calls (similar to KFD interface) */
  347. cgs_alloc_gpu_mem_t alloc_gpu_mem;
  348. cgs_free_gpu_mem_t free_gpu_mem;
  349. cgs_gmap_gpu_mem_t gmap_gpu_mem;
  350. cgs_gunmap_gpu_mem_t gunmap_gpu_mem;
  351. cgs_kmap_gpu_mem_t kmap_gpu_mem;
  352. cgs_kunmap_gpu_mem_t kunmap_gpu_mem;
  353. /* MMIO access */
  354. cgs_read_register_t read_register;
  355. cgs_write_register_t write_register;
  356. cgs_read_ind_register_t read_ind_register;
  357. cgs_write_ind_register_t write_ind_register;
  358. /* PCI resources */
  359. cgs_get_pci_resource_t get_pci_resource;
  360. /* ATOM BIOS */
  361. cgs_atom_get_data_table_t atom_get_data_table;
  362. cgs_atom_get_cmd_table_revs_t atom_get_cmd_table_revs;
  363. cgs_atom_exec_cmd_table_t atom_exec_cmd_table;
  364. /* Firmware Info */
  365. cgs_get_firmware_info get_firmware_info;
  366. cgs_rel_firmware rel_firmware;
  367. /* cg pg interface*/
  368. cgs_set_powergating_state set_powergating_state;
  369. cgs_set_clockgating_state set_clockgating_state;
  370. /* display manager */
  371. cgs_get_active_displays_info get_active_displays_info;
  372. /* notify dpm enabled */
  373. cgs_notify_dpm_enabled notify_dpm_enabled;
  374. /* get system info */
  375. cgs_query_system_info query_system_info;
  376. cgs_is_virtualization_enabled_t is_virtualization_enabled;
  377. cgs_enter_safe_mode enter_safe_mode;
  378. cgs_lock_grbm_idx lock_grbm_idx;
  379. cgs_set_temperature_range set_temperature_range;
  380. };
  381. struct cgs_os_ops; /* To be define in OS-specific CGS header */
  382. struct cgs_device
  383. {
  384. const struct cgs_ops *ops;
  385. const struct cgs_os_ops *os_ops;
  386. /* to be embedded at the start of driver private structure */
  387. };
  388. /* Convenience macros that make CGS indirect function calls look like
  389. * normal function calls */
  390. #define CGS_CALL(func,dev,...) \
  391. (((struct cgs_device *)dev)->ops->func(dev, ##__VA_ARGS__))
  392. #define CGS_OS_CALL(func,dev,...) \
  393. (((struct cgs_device *)dev)->os_ops->func(dev, ##__VA_ARGS__))
  394. #define cgs_alloc_gpu_mem(dev,type,size,align,handle) \
  395. CGS_CALL(alloc_gpu_mem,dev,type,size,align,handle)
  396. #define cgs_free_gpu_mem(dev,handle) \
  397. CGS_CALL(free_gpu_mem,dev,handle)
  398. #define cgs_gmap_gpu_mem(dev,handle,mcaddr) \
  399. CGS_CALL(gmap_gpu_mem,dev,handle,mcaddr)
  400. #define cgs_gunmap_gpu_mem(dev,handle) \
  401. CGS_CALL(gunmap_gpu_mem,dev,handle)
  402. #define cgs_kmap_gpu_mem(dev,handle,map) \
  403. CGS_CALL(kmap_gpu_mem,dev,handle,map)
  404. #define cgs_kunmap_gpu_mem(dev,handle) \
  405. CGS_CALL(kunmap_gpu_mem,dev,handle)
  406. #define cgs_read_register(dev,offset) \
  407. CGS_CALL(read_register,dev,offset)
  408. #define cgs_write_register(dev,offset,value) \
  409. CGS_CALL(write_register,dev,offset,value)
  410. #define cgs_read_ind_register(dev,space,index) \
  411. CGS_CALL(read_ind_register,dev,space,index)
  412. #define cgs_write_ind_register(dev,space,index,value) \
  413. CGS_CALL(write_ind_register,dev,space,index,value)
  414. #define cgs_atom_get_data_table(dev,table,size,frev,crev) \
  415. CGS_CALL(atom_get_data_table,dev,table,size,frev,crev)
  416. #define cgs_atom_get_cmd_table_revs(dev,table,frev,crev) \
  417. CGS_CALL(atom_get_cmd_table_revs,dev,table,frev,crev)
  418. #define cgs_atom_exec_cmd_table(dev,table,args) \
  419. CGS_CALL(atom_exec_cmd_table,dev,table,args)
  420. #define cgs_get_firmware_info(dev, type, info) \
  421. CGS_CALL(get_firmware_info, dev, type, info)
  422. #define cgs_rel_firmware(dev, type) \
  423. CGS_CALL(rel_firmware, dev, type)
  424. #define cgs_set_powergating_state(dev, block_type, state) \
  425. CGS_CALL(set_powergating_state, dev, block_type, state)
  426. #define cgs_set_clockgating_state(dev, block_type, state) \
  427. CGS_CALL(set_clockgating_state, dev, block_type, state)
  428. #define cgs_notify_dpm_enabled(dev, enabled) \
  429. CGS_CALL(notify_dpm_enabled, dev, enabled)
  430. #define cgs_get_active_displays_info(dev, info) \
  431. CGS_CALL(get_active_displays_info, dev, info)
  432. #define cgs_query_system_info(dev, sys_info) \
  433. CGS_CALL(query_system_info, dev, sys_info)
  434. #define cgs_get_pci_resource(cgs_device, resource_type, size, offset, \
  435. resource_base) \
  436. CGS_CALL(get_pci_resource, cgs_device, resource_type, size, offset, \
  437. resource_base)
  438. #define cgs_is_virtualization_enabled(cgs_device) \
  439. CGS_CALL(is_virtualization_enabled, cgs_device)
  440. #define cgs_enter_safe_mode(cgs_device, en) \
  441. CGS_CALL(enter_safe_mode, cgs_device, en)
  442. #define cgs_lock_grbm_idx(cgs_device, lock) \
  443. CGS_CALL(lock_grbm_idx, cgs_device, lock)
  444. #define cgs_set_temperature_range(dev, min_temp, max_temp) \
  445. CGS_CALL(set_temperature_range, dev, min_temp, max_temp)
  446. #endif /* _CGS_COMMON_H */