amdgpu_cgs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. #include <linux/list.h>
  25. #include <linux/slab.h>
  26. #include <linux/pci.h>
  27. #include <drm/drmP.h>
  28. #include <linux/firmware.h>
  29. #include <drm/amdgpu_drm.h>
  30. #include "amdgpu.h"
  31. #include "cgs_linux.h"
  32. #include "atom.h"
  33. #include "amdgpu_ucode.h"
  34. struct amdgpu_cgs_device {
  35. struct cgs_device base;
  36. struct amdgpu_device *adev;
  37. };
  38. #define CGS_FUNC_ADEV \
  39. struct amdgpu_device *adev = \
  40. ((struct amdgpu_cgs_device *)cgs_device)->adev
  41. static int amdgpu_cgs_gpu_mem_info(void *cgs_device, enum cgs_gpu_mem_type type,
  42. uint64_t *mc_start, uint64_t *mc_size,
  43. uint64_t *mem_size)
  44. {
  45. CGS_FUNC_ADEV;
  46. switch(type) {
  47. case CGS_GPU_MEM_TYPE__VISIBLE_CONTIG_FB:
  48. case CGS_GPU_MEM_TYPE__VISIBLE_FB:
  49. *mc_start = 0;
  50. *mc_size = adev->mc.visible_vram_size;
  51. *mem_size = adev->mc.visible_vram_size - adev->vram_pin_size;
  52. break;
  53. case CGS_GPU_MEM_TYPE__INVISIBLE_CONTIG_FB:
  54. case CGS_GPU_MEM_TYPE__INVISIBLE_FB:
  55. *mc_start = adev->mc.visible_vram_size;
  56. *mc_size = adev->mc.real_vram_size - adev->mc.visible_vram_size;
  57. *mem_size = *mc_size;
  58. break;
  59. case CGS_GPU_MEM_TYPE__GART_CACHEABLE:
  60. case CGS_GPU_MEM_TYPE__GART_WRITECOMBINE:
  61. *mc_start = adev->mc.gtt_start;
  62. *mc_size = adev->mc.gtt_size;
  63. *mem_size = adev->mc.gtt_size - adev->gart_pin_size;
  64. break;
  65. default:
  66. return -EINVAL;
  67. }
  68. return 0;
  69. }
  70. static int amdgpu_cgs_gmap_kmem(void *cgs_device, void *kmem,
  71. uint64_t size,
  72. uint64_t min_offset, uint64_t max_offset,
  73. cgs_handle_t *kmem_handle, uint64_t *mcaddr)
  74. {
  75. CGS_FUNC_ADEV;
  76. int ret;
  77. struct amdgpu_bo *bo;
  78. struct page *kmem_page = vmalloc_to_page(kmem);
  79. int npages = ALIGN(size, PAGE_SIZE) >> PAGE_SHIFT;
  80. struct sg_table *sg = drm_prime_pages_to_sg(&kmem_page, npages);
  81. ret = amdgpu_bo_create(adev, size, PAGE_SIZE, false,
  82. AMDGPU_GEM_DOMAIN_GTT, 0, sg, NULL, &bo);
  83. if (ret)
  84. return ret;
  85. ret = amdgpu_bo_reserve(bo, false);
  86. if (unlikely(ret != 0))
  87. return ret;
  88. /* pin buffer into GTT */
  89. ret = amdgpu_bo_pin_restricted(bo, AMDGPU_GEM_DOMAIN_GTT,
  90. min_offset, max_offset, mcaddr);
  91. amdgpu_bo_unreserve(bo);
  92. *kmem_handle = (cgs_handle_t)bo;
  93. return ret;
  94. }
  95. static int amdgpu_cgs_gunmap_kmem(void *cgs_device, cgs_handle_t kmem_handle)
  96. {
  97. struct amdgpu_bo *obj = (struct amdgpu_bo *)kmem_handle;
  98. if (obj) {
  99. int r = amdgpu_bo_reserve(obj, false);
  100. if (likely(r == 0)) {
  101. amdgpu_bo_unpin(obj);
  102. amdgpu_bo_unreserve(obj);
  103. }
  104. amdgpu_bo_unref(&obj);
  105. }
  106. return 0;
  107. }
  108. static int amdgpu_cgs_alloc_gpu_mem(void *cgs_device,
  109. enum cgs_gpu_mem_type type,
  110. uint64_t size, uint64_t align,
  111. uint64_t min_offset, uint64_t max_offset,
  112. cgs_handle_t *handle)
  113. {
  114. CGS_FUNC_ADEV;
  115. uint16_t flags = 0;
  116. int ret = 0;
  117. uint32_t domain = 0;
  118. struct amdgpu_bo *obj;
  119. struct ttm_placement placement;
  120. struct ttm_place place;
  121. if (min_offset > max_offset) {
  122. BUG_ON(1);
  123. return -EINVAL;
  124. }
  125. /* fail if the alignment is not a power of 2 */
  126. if (((align != 1) && (align & (align - 1)))
  127. || size == 0 || align == 0)
  128. return -EINVAL;
  129. switch(type) {
  130. case CGS_GPU_MEM_TYPE__VISIBLE_CONTIG_FB:
  131. case CGS_GPU_MEM_TYPE__VISIBLE_FB:
  132. flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  133. domain = AMDGPU_GEM_DOMAIN_VRAM;
  134. if (max_offset > adev->mc.real_vram_size)
  135. return -EINVAL;
  136. place.fpfn = min_offset >> PAGE_SHIFT;
  137. place.lpfn = max_offset >> PAGE_SHIFT;
  138. place.flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
  139. TTM_PL_FLAG_VRAM;
  140. break;
  141. case CGS_GPU_MEM_TYPE__INVISIBLE_CONTIG_FB:
  142. case CGS_GPU_MEM_TYPE__INVISIBLE_FB:
  143. flags = AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
  144. domain = AMDGPU_GEM_DOMAIN_VRAM;
  145. if (adev->mc.visible_vram_size < adev->mc.real_vram_size) {
  146. place.fpfn =
  147. max(min_offset, adev->mc.visible_vram_size) >> PAGE_SHIFT;
  148. place.lpfn =
  149. min(max_offset, adev->mc.real_vram_size) >> PAGE_SHIFT;
  150. place.flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
  151. TTM_PL_FLAG_VRAM;
  152. }
  153. break;
  154. case CGS_GPU_MEM_TYPE__GART_CACHEABLE:
  155. domain = AMDGPU_GEM_DOMAIN_GTT;
  156. place.fpfn = min_offset >> PAGE_SHIFT;
  157. place.lpfn = max_offset >> PAGE_SHIFT;
  158. place.flags = TTM_PL_FLAG_CACHED | TTM_PL_FLAG_TT;
  159. break;
  160. case CGS_GPU_MEM_TYPE__GART_WRITECOMBINE:
  161. flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC;
  162. domain = AMDGPU_GEM_DOMAIN_GTT;
  163. place.fpfn = min_offset >> PAGE_SHIFT;
  164. place.lpfn = max_offset >> PAGE_SHIFT;
  165. place.flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_TT |
  166. TTM_PL_FLAG_UNCACHED;
  167. break;
  168. default:
  169. return -EINVAL;
  170. }
  171. *handle = 0;
  172. placement.placement = &place;
  173. placement.num_placement = 1;
  174. placement.busy_placement = &place;
  175. placement.num_busy_placement = 1;
  176. ret = amdgpu_bo_create_restricted(adev, size, PAGE_SIZE,
  177. true, domain, flags,
  178. NULL, &placement, NULL,
  179. &obj);
  180. if (ret) {
  181. DRM_ERROR("(%d) bo create failed\n", ret);
  182. return ret;
  183. }
  184. *handle = (cgs_handle_t)obj;
  185. return ret;
  186. }
  187. static int amdgpu_cgs_free_gpu_mem(void *cgs_device, cgs_handle_t handle)
  188. {
  189. struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
  190. if (obj) {
  191. int r = amdgpu_bo_reserve(obj, false);
  192. if (likely(r == 0)) {
  193. amdgpu_bo_kunmap(obj);
  194. amdgpu_bo_unpin(obj);
  195. amdgpu_bo_unreserve(obj);
  196. }
  197. amdgpu_bo_unref(&obj);
  198. }
  199. return 0;
  200. }
  201. static int amdgpu_cgs_gmap_gpu_mem(void *cgs_device, cgs_handle_t handle,
  202. uint64_t *mcaddr)
  203. {
  204. int r;
  205. u64 min_offset, max_offset;
  206. struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
  207. WARN_ON_ONCE(obj->placement.num_placement > 1);
  208. min_offset = obj->placements[0].fpfn << PAGE_SHIFT;
  209. max_offset = obj->placements[0].lpfn << PAGE_SHIFT;
  210. r = amdgpu_bo_reserve(obj, false);
  211. if (unlikely(r != 0))
  212. return r;
  213. r = amdgpu_bo_pin_restricted(obj, AMDGPU_GEM_DOMAIN_GTT,
  214. min_offset, max_offset, mcaddr);
  215. amdgpu_bo_unreserve(obj);
  216. return r;
  217. }
  218. static int amdgpu_cgs_gunmap_gpu_mem(void *cgs_device, cgs_handle_t handle)
  219. {
  220. int r;
  221. struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
  222. r = amdgpu_bo_reserve(obj, false);
  223. if (unlikely(r != 0))
  224. return r;
  225. r = amdgpu_bo_unpin(obj);
  226. amdgpu_bo_unreserve(obj);
  227. return r;
  228. }
  229. static int amdgpu_cgs_kmap_gpu_mem(void *cgs_device, cgs_handle_t handle,
  230. void **map)
  231. {
  232. int r;
  233. struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
  234. r = amdgpu_bo_reserve(obj, false);
  235. if (unlikely(r != 0))
  236. return r;
  237. r = amdgpu_bo_kmap(obj, map);
  238. amdgpu_bo_unreserve(obj);
  239. return r;
  240. }
  241. static int amdgpu_cgs_kunmap_gpu_mem(void *cgs_device, cgs_handle_t handle)
  242. {
  243. int r;
  244. struct amdgpu_bo *obj = (struct amdgpu_bo *)handle;
  245. r = amdgpu_bo_reserve(obj, false);
  246. if (unlikely(r != 0))
  247. return r;
  248. amdgpu_bo_kunmap(obj);
  249. amdgpu_bo_unreserve(obj);
  250. return r;
  251. }
  252. static uint32_t amdgpu_cgs_read_register(void *cgs_device, unsigned offset)
  253. {
  254. CGS_FUNC_ADEV;
  255. return RREG32(offset);
  256. }
  257. static void amdgpu_cgs_write_register(void *cgs_device, unsigned offset,
  258. uint32_t value)
  259. {
  260. CGS_FUNC_ADEV;
  261. WREG32(offset, value);
  262. }
  263. static uint32_t amdgpu_cgs_read_ind_register(void *cgs_device,
  264. enum cgs_ind_reg space,
  265. unsigned index)
  266. {
  267. CGS_FUNC_ADEV;
  268. switch (space) {
  269. case CGS_IND_REG__MMIO:
  270. return RREG32_IDX(index);
  271. case CGS_IND_REG__PCIE:
  272. return RREG32_PCIE(index);
  273. case CGS_IND_REG__SMC:
  274. return RREG32_SMC(index);
  275. case CGS_IND_REG__UVD_CTX:
  276. return RREG32_UVD_CTX(index);
  277. case CGS_IND_REG__DIDT:
  278. return RREG32_DIDT(index);
  279. case CGS_IND_REG__AUDIO_ENDPT:
  280. DRM_ERROR("audio endpt register access not implemented.\n");
  281. return 0;
  282. }
  283. WARN(1, "Invalid indirect register space");
  284. return 0;
  285. }
  286. static void amdgpu_cgs_write_ind_register(void *cgs_device,
  287. enum cgs_ind_reg space,
  288. unsigned index, uint32_t value)
  289. {
  290. CGS_FUNC_ADEV;
  291. switch (space) {
  292. case CGS_IND_REG__MMIO:
  293. return WREG32_IDX(index, value);
  294. case CGS_IND_REG__PCIE:
  295. return WREG32_PCIE(index, value);
  296. case CGS_IND_REG__SMC:
  297. return WREG32_SMC(index, value);
  298. case CGS_IND_REG__UVD_CTX:
  299. return WREG32_UVD_CTX(index, value);
  300. case CGS_IND_REG__DIDT:
  301. return WREG32_DIDT(index, value);
  302. case CGS_IND_REG__AUDIO_ENDPT:
  303. DRM_ERROR("audio endpt register access not implemented.\n");
  304. return;
  305. }
  306. WARN(1, "Invalid indirect register space");
  307. }
  308. static uint8_t amdgpu_cgs_read_pci_config_byte(void *cgs_device, unsigned addr)
  309. {
  310. CGS_FUNC_ADEV;
  311. uint8_t val;
  312. int ret = pci_read_config_byte(adev->pdev, addr, &val);
  313. if (WARN(ret, "pci_read_config_byte error"))
  314. return 0;
  315. return val;
  316. }
  317. static uint16_t amdgpu_cgs_read_pci_config_word(void *cgs_device, unsigned addr)
  318. {
  319. CGS_FUNC_ADEV;
  320. uint16_t val;
  321. int ret = pci_read_config_word(adev->pdev, addr, &val);
  322. if (WARN(ret, "pci_read_config_word error"))
  323. return 0;
  324. return val;
  325. }
  326. static uint32_t amdgpu_cgs_read_pci_config_dword(void *cgs_device,
  327. unsigned addr)
  328. {
  329. CGS_FUNC_ADEV;
  330. uint32_t val;
  331. int ret = pci_read_config_dword(adev->pdev, addr, &val);
  332. if (WARN(ret, "pci_read_config_dword error"))
  333. return 0;
  334. return val;
  335. }
  336. static void amdgpu_cgs_write_pci_config_byte(void *cgs_device, unsigned addr,
  337. uint8_t value)
  338. {
  339. CGS_FUNC_ADEV;
  340. int ret = pci_write_config_byte(adev->pdev, addr, value);
  341. WARN(ret, "pci_write_config_byte error");
  342. }
  343. static void amdgpu_cgs_write_pci_config_word(void *cgs_device, unsigned addr,
  344. uint16_t value)
  345. {
  346. CGS_FUNC_ADEV;
  347. int ret = pci_write_config_word(adev->pdev, addr, value);
  348. WARN(ret, "pci_write_config_word error");
  349. }
  350. static void amdgpu_cgs_write_pci_config_dword(void *cgs_device, unsigned addr,
  351. uint32_t value)
  352. {
  353. CGS_FUNC_ADEV;
  354. int ret = pci_write_config_dword(adev->pdev, addr, value);
  355. WARN(ret, "pci_write_config_dword error");
  356. }
  357. static const void *amdgpu_cgs_atom_get_data_table(void *cgs_device,
  358. unsigned table, uint16_t *size,
  359. uint8_t *frev, uint8_t *crev)
  360. {
  361. CGS_FUNC_ADEV;
  362. uint16_t data_start;
  363. if (amdgpu_atom_parse_data_header(
  364. adev->mode_info.atom_context, table, size,
  365. frev, crev, &data_start))
  366. return (uint8_t*)adev->mode_info.atom_context->bios +
  367. data_start;
  368. return NULL;
  369. }
  370. static int amdgpu_cgs_atom_get_cmd_table_revs(void *cgs_device, unsigned table,
  371. uint8_t *frev, uint8_t *crev)
  372. {
  373. CGS_FUNC_ADEV;
  374. if (amdgpu_atom_parse_cmd_header(
  375. adev->mode_info.atom_context, table,
  376. frev, crev))
  377. return 0;
  378. return -EINVAL;
  379. }
  380. static int amdgpu_cgs_atom_exec_cmd_table(void *cgs_device, unsigned table,
  381. void *args)
  382. {
  383. CGS_FUNC_ADEV;
  384. return amdgpu_atom_execute_table(
  385. adev->mode_info.atom_context, table, args);
  386. }
  387. static int amdgpu_cgs_create_pm_request(void *cgs_device, cgs_handle_t *request)
  388. {
  389. /* TODO */
  390. return 0;
  391. }
  392. static int amdgpu_cgs_destroy_pm_request(void *cgs_device, cgs_handle_t request)
  393. {
  394. /* TODO */
  395. return 0;
  396. }
  397. static int amdgpu_cgs_set_pm_request(void *cgs_device, cgs_handle_t request,
  398. int active)
  399. {
  400. /* TODO */
  401. return 0;
  402. }
  403. static int amdgpu_cgs_pm_request_clock(void *cgs_device, cgs_handle_t request,
  404. enum cgs_clock clock, unsigned freq)
  405. {
  406. /* TODO */
  407. return 0;
  408. }
  409. static int amdgpu_cgs_pm_request_engine(void *cgs_device, cgs_handle_t request,
  410. enum cgs_engine engine, int powered)
  411. {
  412. /* TODO */
  413. return 0;
  414. }
  415. static int amdgpu_cgs_pm_query_clock_limits(void *cgs_device,
  416. enum cgs_clock clock,
  417. struct cgs_clock_limits *limits)
  418. {
  419. /* TODO */
  420. return 0;
  421. }
  422. static int amdgpu_cgs_set_camera_voltages(void *cgs_device, uint32_t mask,
  423. const uint32_t *voltages)
  424. {
  425. DRM_ERROR("not implemented");
  426. return -EPERM;
  427. }
  428. struct cgs_irq_params {
  429. unsigned src_id;
  430. cgs_irq_source_set_func_t set;
  431. cgs_irq_handler_func_t handler;
  432. void *private_data;
  433. };
  434. static int cgs_set_irq_state(struct amdgpu_device *adev,
  435. struct amdgpu_irq_src *src,
  436. unsigned type,
  437. enum amdgpu_interrupt_state state)
  438. {
  439. struct cgs_irq_params *irq_params =
  440. (struct cgs_irq_params *)src->data;
  441. if (!irq_params)
  442. return -EINVAL;
  443. if (!irq_params->set)
  444. return -EINVAL;
  445. return irq_params->set(irq_params->private_data,
  446. irq_params->src_id,
  447. type,
  448. (int)state);
  449. }
  450. static int cgs_process_irq(struct amdgpu_device *adev,
  451. struct amdgpu_irq_src *source,
  452. struct amdgpu_iv_entry *entry)
  453. {
  454. struct cgs_irq_params *irq_params =
  455. (struct cgs_irq_params *)source->data;
  456. if (!irq_params)
  457. return -EINVAL;
  458. if (!irq_params->handler)
  459. return -EINVAL;
  460. return irq_params->handler(irq_params->private_data,
  461. irq_params->src_id,
  462. entry->iv_entry);
  463. }
  464. static const struct amdgpu_irq_src_funcs cgs_irq_funcs = {
  465. .set = cgs_set_irq_state,
  466. .process = cgs_process_irq,
  467. };
  468. static int amdgpu_cgs_add_irq_source(void *cgs_device, unsigned src_id,
  469. unsigned num_types,
  470. cgs_irq_source_set_func_t set,
  471. cgs_irq_handler_func_t handler,
  472. void *private_data)
  473. {
  474. CGS_FUNC_ADEV;
  475. int ret = 0;
  476. struct cgs_irq_params *irq_params;
  477. struct amdgpu_irq_src *source =
  478. kzalloc(sizeof(struct amdgpu_irq_src), GFP_KERNEL);
  479. if (!source)
  480. return -ENOMEM;
  481. irq_params =
  482. kzalloc(sizeof(struct cgs_irq_params), GFP_KERNEL);
  483. if (!irq_params) {
  484. kfree(source);
  485. return -ENOMEM;
  486. }
  487. source->num_types = num_types;
  488. source->funcs = &cgs_irq_funcs;
  489. irq_params->src_id = src_id;
  490. irq_params->set = set;
  491. irq_params->handler = handler;
  492. irq_params->private_data = private_data;
  493. source->data = (void *)irq_params;
  494. ret = amdgpu_irq_add_id(adev, src_id, source);
  495. if (ret) {
  496. kfree(irq_params);
  497. kfree(source);
  498. }
  499. return ret;
  500. }
  501. static int amdgpu_cgs_irq_get(void *cgs_device, unsigned src_id, unsigned type)
  502. {
  503. CGS_FUNC_ADEV;
  504. return amdgpu_irq_get(adev, adev->irq.sources[src_id], type);
  505. }
  506. static int amdgpu_cgs_irq_put(void *cgs_device, unsigned src_id, unsigned type)
  507. {
  508. CGS_FUNC_ADEV;
  509. return amdgpu_irq_put(adev, adev->irq.sources[src_id], type);
  510. }
  511. int amdgpu_cgs_set_clockgating_state(void *cgs_device,
  512. enum amd_ip_block_type block_type,
  513. enum amd_clockgating_state state)
  514. {
  515. CGS_FUNC_ADEV;
  516. int i, r = -1;
  517. for (i = 0; i < adev->num_ip_blocks; i++) {
  518. if (!adev->ip_block_status[i].valid)
  519. continue;
  520. if (adev->ip_blocks[i].type == block_type) {
  521. r = adev->ip_blocks[i].funcs->set_clockgating_state(
  522. (void *)adev,
  523. state);
  524. break;
  525. }
  526. }
  527. return r;
  528. }
  529. int amdgpu_cgs_set_powergating_state(void *cgs_device,
  530. enum amd_ip_block_type block_type,
  531. enum amd_powergating_state state)
  532. {
  533. CGS_FUNC_ADEV;
  534. int i, r = -1;
  535. for (i = 0; i < adev->num_ip_blocks; i++) {
  536. if (!adev->ip_block_status[i].valid)
  537. continue;
  538. if (adev->ip_blocks[i].type == block_type) {
  539. r = adev->ip_blocks[i].funcs->set_powergating_state(
  540. (void *)adev,
  541. state);
  542. break;
  543. }
  544. }
  545. return r;
  546. }
  547. static uint32_t fw_type_convert(void *cgs_device, uint32_t fw_type)
  548. {
  549. CGS_FUNC_ADEV;
  550. enum AMDGPU_UCODE_ID result = AMDGPU_UCODE_ID_MAXIMUM;
  551. switch (fw_type) {
  552. case CGS_UCODE_ID_SDMA0:
  553. result = AMDGPU_UCODE_ID_SDMA0;
  554. break;
  555. case CGS_UCODE_ID_SDMA1:
  556. result = AMDGPU_UCODE_ID_SDMA1;
  557. break;
  558. case CGS_UCODE_ID_CP_CE:
  559. result = AMDGPU_UCODE_ID_CP_CE;
  560. break;
  561. case CGS_UCODE_ID_CP_PFP:
  562. result = AMDGPU_UCODE_ID_CP_PFP;
  563. break;
  564. case CGS_UCODE_ID_CP_ME:
  565. result = AMDGPU_UCODE_ID_CP_ME;
  566. break;
  567. case CGS_UCODE_ID_CP_MEC:
  568. case CGS_UCODE_ID_CP_MEC_JT1:
  569. result = AMDGPU_UCODE_ID_CP_MEC1;
  570. break;
  571. case CGS_UCODE_ID_CP_MEC_JT2:
  572. if (adev->asic_type == CHIP_TONGA)
  573. result = AMDGPU_UCODE_ID_CP_MEC2;
  574. else if (adev->asic_type == CHIP_CARRIZO)
  575. result = AMDGPU_UCODE_ID_CP_MEC1;
  576. break;
  577. case CGS_UCODE_ID_RLC_G:
  578. result = AMDGPU_UCODE_ID_RLC_G;
  579. break;
  580. default:
  581. DRM_ERROR("Firmware type not supported\n");
  582. }
  583. return result;
  584. }
  585. static int amdgpu_cgs_get_firmware_info(void *cgs_device,
  586. enum cgs_ucode_id type,
  587. struct cgs_firmware_info *info)
  588. {
  589. CGS_FUNC_ADEV;
  590. if (CGS_UCODE_ID_SMU != type) {
  591. uint64_t gpu_addr;
  592. uint32_t data_size;
  593. const struct gfx_firmware_header_v1_0 *header;
  594. enum AMDGPU_UCODE_ID id;
  595. struct amdgpu_firmware_info *ucode;
  596. id = fw_type_convert(cgs_device, type);
  597. ucode = &adev->firmware.ucode[id];
  598. if (ucode->fw == NULL)
  599. return -EINVAL;
  600. gpu_addr = ucode->mc_addr;
  601. header = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
  602. data_size = le32_to_cpu(header->header.ucode_size_bytes);
  603. if ((type == CGS_UCODE_ID_CP_MEC_JT1) ||
  604. (type == CGS_UCODE_ID_CP_MEC_JT2)) {
  605. gpu_addr += le32_to_cpu(header->jt_offset) << 2;
  606. data_size = le32_to_cpu(header->jt_size) << 2;
  607. }
  608. info->mc_addr = gpu_addr;
  609. info->image_size = data_size;
  610. info->version = (uint16_t)le32_to_cpu(header->header.ucode_version);
  611. info->feature_version = (uint16_t)le32_to_cpu(header->ucode_feature_version);
  612. } else {
  613. char fw_name[30] = {0};
  614. int err = 0;
  615. uint32_t ucode_size;
  616. uint32_t ucode_start_address;
  617. const uint8_t *src;
  618. const struct smc_firmware_header_v1_0 *hdr;
  619. switch (adev->asic_type) {
  620. case CHIP_TONGA:
  621. strcpy(fw_name, "amdgpu/tonga_smc.bin");
  622. break;
  623. default:
  624. DRM_ERROR("SMC firmware not supported\n");
  625. return -EINVAL;
  626. }
  627. err = request_firmware(&adev->pm.fw, fw_name, adev->dev);
  628. if (err) {
  629. DRM_ERROR("Failed to request firmware\n");
  630. return err;
  631. }
  632. err = amdgpu_ucode_validate(adev->pm.fw);
  633. if (err) {
  634. DRM_ERROR("Failed to load firmware \"%s\"", fw_name);
  635. release_firmware(adev->pm.fw);
  636. adev->pm.fw = NULL;
  637. return err;
  638. }
  639. hdr = (const struct smc_firmware_header_v1_0 *) adev->pm.fw->data;
  640. adev->pm.fw_version = le32_to_cpu(hdr->header.ucode_version);
  641. ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes);
  642. ucode_start_address = le32_to_cpu(hdr->ucode_start_addr);
  643. src = (const uint8_t *)(adev->pm.fw->data +
  644. le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  645. info->version = adev->pm.fw_version;
  646. info->image_size = ucode_size;
  647. info->kptr = (void *)src;
  648. }
  649. return 0;
  650. }
  651. static const struct cgs_ops amdgpu_cgs_ops = {
  652. amdgpu_cgs_gpu_mem_info,
  653. amdgpu_cgs_gmap_kmem,
  654. amdgpu_cgs_gunmap_kmem,
  655. amdgpu_cgs_alloc_gpu_mem,
  656. amdgpu_cgs_free_gpu_mem,
  657. amdgpu_cgs_gmap_gpu_mem,
  658. amdgpu_cgs_gunmap_gpu_mem,
  659. amdgpu_cgs_kmap_gpu_mem,
  660. amdgpu_cgs_kunmap_gpu_mem,
  661. amdgpu_cgs_read_register,
  662. amdgpu_cgs_write_register,
  663. amdgpu_cgs_read_ind_register,
  664. amdgpu_cgs_write_ind_register,
  665. amdgpu_cgs_read_pci_config_byte,
  666. amdgpu_cgs_read_pci_config_word,
  667. amdgpu_cgs_read_pci_config_dword,
  668. amdgpu_cgs_write_pci_config_byte,
  669. amdgpu_cgs_write_pci_config_word,
  670. amdgpu_cgs_write_pci_config_dword,
  671. amdgpu_cgs_atom_get_data_table,
  672. amdgpu_cgs_atom_get_cmd_table_revs,
  673. amdgpu_cgs_atom_exec_cmd_table,
  674. amdgpu_cgs_create_pm_request,
  675. amdgpu_cgs_destroy_pm_request,
  676. amdgpu_cgs_set_pm_request,
  677. amdgpu_cgs_pm_request_clock,
  678. amdgpu_cgs_pm_request_engine,
  679. amdgpu_cgs_pm_query_clock_limits,
  680. amdgpu_cgs_set_camera_voltages,
  681. amdgpu_cgs_get_firmware_info,
  682. amdgpu_cgs_set_powergating_state,
  683. amdgpu_cgs_set_clockgating_state
  684. };
  685. static const struct cgs_os_ops amdgpu_cgs_os_ops = {
  686. amdgpu_cgs_add_irq_source,
  687. amdgpu_cgs_irq_get,
  688. amdgpu_cgs_irq_put
  689. };
  690. void *amdgpu_cgs_create_device(struct amdgpu_device *adev)
  691. {
  692. struct amdgpu_cgs_device *cgs_device =
  693. kmalloc(sizeof(*cgs_device), GFP_KERNEL);
  694. if (!cgs_device) {
  695. DRM_ERROR("Couldn't allocate CGS device structure\n");
  696. return NULL;
  697. }
  698. cgs_device->base.ops = &amdgpu_cgs_ops;
  699. cgs_device->base.os_ops = &amdgpu_cgs_os_ops;
  700. cgs_device->adev = adev;
  701. return cgs_device;
  702. }
  703. void amdgpu_cgs_destroy_device(void *cgs_device)
  704. {
  705. kfree(cgs_device);
  706. }