gmc_v7_0.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. /*
  2. * Copyright 2014 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. #include <linux/firmware.h>
  24. #include <drm/drmP.h>
  25. #include "amdgpu.h"
  26. #include "cikd.h"
  27. #include "cik.h"
  28. #include "gmc_v7_0.h"
  29. #include "amdgpu_ucode.h"
  30. #include "bif/bif_4_1_d.h"
  31. #include "bif/bif_4_1_sh_mask.h"
  32. #include "gmc/gmc_7_1_d.h"
  33. #include "gmc/gmc_7_1_sh_mask.h"
  34. #include "oss/oss_2_0_d.h"
  35. #include "oss/oss_2_0_sh_mask.h"
  36. #include "dce/dce_8_0_d.h"
  37. #include "dce/dce_8_0_sh_mask.h"
  38. #include "amdgpu_atombios.h"
  39. static void gmc_v7_0_set_gart_funcs(struct amdgpu_device *adev);
  40. static void gmc_v7_0_set_irq_funcs(struct amdgpu_device *adev);
  41. static int gmc_v7_0_wait_for_idle(void *handle);
  42. MODULE_FIRMWARE("radeon/bonaire_mc.bin");
  43. MODULE_FIRMWARE("radeon/hawaii_mc.bin");
  44. MODULE_FIRMWARE("amdgpu/topaz_mc.bin");
  45. static const u32 golden_settings_iceland_a11[] =
  46. {
  47. mmVM_PRT_APERTURE0_LOW_ADDR, 0x0fffffff, 0x0fffffff,
  48. mmVM_PRT_APERTURE1_LOW_ADDR, 0x0fffffff, 0x0fffffff,
  49. mmVM_PRT_APERTURE2_LOW_ADDR, 0x0fffffff, 0x0fffffff,
  50. mmVM_PRT_APERTURE3_LOW_ADDR, 0x0fffffff, 0x0fffffff
  51. };
  52. static const u32 iceland_mgcg_cgcg_init[] =
  53. {
  54. mmMC_MEM_POWER_LS, 0xffffffff, 0x00000104
  55. };
  56. static void gmc_v7_0_init_golden_registers(struct amdgpu_device *adev)
  57. {
  58. switch (adev->asic_type) {
  59. case CHIP_TOPAZ:
  60. amdgpu_program_register_sequence(adev,
  61. iceland_mgcg_cgcg_init,
  62. ARRAY_SIZE(iceland_mgcg_cgcg_init));
  63. amdgpu_program_register_sequence(adev,
  64. golden_settings_iceland_a11,
  65. ARRAY_SIZE(golden_settings_iceland_a11));
  66. break;
  67. default:
  68. break;
  69. }
  70. }
  71. static void gmc_v7_0_mc_stop(struct amdgpu_device *adev)
  72. {
  73. u32 blackout;
  74. gmc_v7_0_wait_for_idle((void *)adev);
  75. blackout = RREG32(mmMC_SHARED_BLACKOUT_CNTL);
  76. if (REG_GET_FIELD(blackout, MC_SHARED_BLACKOUT_CNTL, BLACKOUT_MODE) != 1) {
  77. /* Block CPU access */
  78. WREG32(mmBIF_FB_EN, 0);
  79. /* blackout the MC */
  80. blackout = REG_SET_FIELD(blackout,
  81. MC_SHARED_BLACKOUT_CNTL, BLACKOUT_MODE, 0);
  82. WREG32(mmMC_SHARED_BLACKOUT_CNTL, blackout | 1);
  83. }
  84. /* wait for the MC to settle */
  85. udelay(100);
  86. }
  87. static void gmc_v7_0_mc_resume(struct amdgpu_device *adev)
  88. {
  89. u32 tmp;
  90. /* unblackout the MC */
  91. tmp = RREG32(mmMC_SHARED_BLACKOUT_CNTL);
  92. tmp = REG_SET_FIELD(tmp, MC_SHARED_BLACKOUT_CNTL, BLACKOUT_MODE, 0);
  93. WREG32(mmMC_SHARED_BLACKOUT_CNTL, tmp);
  94. /* allow CPU access */
  95. tmp = REG_SET_FIELD(0, BIF_FB_EN, FB_READ_EN, 1);
  96. tmp = REG_SET_FIELD(tmp, BIF_FB_EN, FB_WRITE_EN, 1);
  97. WREG32(mmBIF_FB_EN, tmp);
  98. }
  99. /**
  100. * gmc_v7_0_init_microcode - load ucode images from disk
  101. *
  102. * @adev: amdgpu_device pointer
  103. *
  104. * Use the firmware interface to load the ucode images into
  105. * the driver (not loaded into hw).
  106. * Returns 0 on success, error on failure.
  107. */
  108. static int gmc_v7_0_init_microcode(struct amdgpu_device *adev)
  109. {
  110. const char *chip_name;
  111. char fw_name[30];
  112. int err;
  113. DRM_DEBUG("\n");
  114. switch (adev->asic_type) {
  115. case CHIP_BONAIRE:
  116. chip_name = "bonaire";
  117. break;
  118. case CHIP_HAWAII:
  119. chip_name = "hawaii";
  120. break;
  121. case CHIP_TOPAZ:
  122. chip_name = "topaz";
  123. break;
  124. case CHIP_KAVERI:
  125. case CHIP_KABINI:
  126. case CHIP_MULLINS:
  127. return 0;
  128. default: BUG();
  129. }
  130. if (adev->asic_type == CHIP_TOPAZ)
  131. snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mc.bin", chip_name);
  132. else
  133. snprintf(fw_name, sizeof(fw_name), "radeon/%s_mc.bin", chip_name);
  134. err = request_firmware(&adev->mc.fw, fw_name, adev->dev);
  135. if (err)
  136. goto out;
  137. err = amdgpu_ucode_validate(adev->mc.fw);
  138. out:
  139. if (err) {
  140. pr_err("cik_mc: Failed to load firmware \"%s\"\n", fw_name);
  141. release_firmware(adev->mc.fw);
  142. adev->mc.fw = NULL;
  143. }
  144. return err;
  145. }
  146. /**
  147. * gmc_v7_0_mc_load_microcode - load MC ucode into the hw
  148. *
  149. * @adev: amdgpu_device pointer
  150. *
  151. * Load the GDDR MC ucode into the hw (CIK).
  152. * Returns 0 on success, error on failure.
  153. */
  154. static int gmc_v7_0_mc_load_microcode(struct amdgpu_device *adev)
  155. {
  156. const struct mc_firmware_header_v1_0 *hdr;
  157. const __le32 *fw_data = NULL;
  158. const __le32 *io_mc_regs = NULL;
  159. u32 running;
  160. int i, ucode_size, regs_size;
  161. if (!adev->mc.fw)
  162. return -EINVAL;
  163. hdr = (const struct mc_firmware_header_v1_0 *)adev->mc.fw->data;
  164. amdgpu_ucode_print_mc_hdr(&hdr->header);
  165. adev->mc.fw_version = le32_to_cpu(hdr->header.ucode_version);
  166. regs_size = le32_to_cpu(hdr->io_debug_size_bytes) / (4 * 2);
  167. io_mc_regs = (const __le32 *)
  168. (adev->mc.fw->data + le32_to_cpu(hdr->io_debug_array_offset_bytes));
  169. ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
  170. fw_data = (const __le32 *)
  171. (adev->mc.fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  172. running = REG_GET_FIELD(RREG32(mmMC_SEQ_SUP_CNTL), MC_SEQ_SUP_CNTL, RUN);
  173. if (running == 0) {
  174. /* reset the engine and set to writable */
  175. WREG32(mmMC_SEQ_SUP_CNTL, 0x00000008);
  176. WREG32(mmMC_SEQ_SUP_CNTL, 0x00000010);
  177. /* load mc io regs */
  178. for (i = 0; i < regs_size; i++) {
  179. WREG32(mmMC_SEQ_IO_DEBUG_INDEX, le32_to_cpup(io_mc_regs++));
  180. WREG32(mmMC_SEQ_IO_DEBUG_DATA, le32_to_cpup(io_mc_regs++));
  181. }
  182. /* load the MC ucode */
  183. for (i = 0; i < ucode_size; i++)
  184. WREG32(mmMC_SEQ_SUP_PGM, le32_to_cpup(fw_data++));
  185. /* put the engine back into the active state */
  186. WREG32(mmMC_SEQ_SUP_CNTL, 0x00000008);
  187. WREG32(mmMC_SEQ_SUP_CNTL, 0x00000004);
  188. WREG32(mmMC_SEQ_SUP_CNTL, 0x00000001);
  189. /* wait for training to complete */
  190. for (i = 0; i < adev->usec_timeout; i++) {
  191. if (REG_GET_FIELD(RREG32(mmMC_SEQ_TRAIN_WAKEUP_CNTL),
  192. MC_SEQ_TRAIN_WAKEUP_CNTL, TRAIN_DONE_D0))
  193. break;
  194. udelay(1);
  195. }
  196. for (i = 0; i < adev->usec_timeout; i++) {
  197. if (REG_GET_FIELD(RREG32(mmMC_SEQ_TRAIN_WAKEUP_CNTL),
  198. MC_SEQ_TRAIN_WAKEUP_CNTL, TRAIN_DONE_D1))
  199. break;
  200. udelay(1);
  201. }
  202. }
  203. return 0;
  204. }
  205. static void gmc_v7_0_vram_gtt_location(struct amdgpu_device *adev,
  206. struct amdgpu_mc *mc)
  207. {
  208. u64 base = RREG32(mmMC_VM_FB_LOCATION) & 0xFFFF;
  209. base <<= 24;
  210. amdgpu_vram_location(adev, &adev->mc, base);
  211. amdgpu_gart_location(adev, mc);
  212. }
  213. /**
  214. * gmc_v7_0_mc_program - program the GPU memory controller
  215. *
  216. * @adev: amdgpu_device pointer
  217. *
  218. * Set the location of vram, gart, and AGP in the GPU's
  219. * physical address space (CIK).
  220. */
  221. static void gmc_v7_0_mc_program(struct amdgpu_device *adev)
  222. {
  223. u32 tmp;
  224. int i, j;
  225. /* Initialize HDP */
  226. for (i = 0, j = 0; i < 32; i++, j += 0x6) {
  227. WREG32((0xb05 + j), 0x00000000);
  228. WREG32((0xb06 + j), 0x00000000);
  229. WREG32((0xb07 + j), 0x00000000);
  230. WREG32((0xb08 + j), 0x00000000);
  231. WREG32((0xb09 + j), 0x00000000);
  232. }
  233. WREG32(mmHDP_REG_COHERENCY_FLUSH_CNTL, 0);
  234. if (gmc_v7_0_wait_for_idle((void *)adev)) {
  235. dev_warn(adev->dev, "Wait for MC idle timedout !\n");
  236. }
  237. if (adev->mode_info.num_crtc) {
  238. /* Lockout access through VGA aperture*/
  239. tmp = RREG32(mmVGA_HDP_CONTROL);
  240. tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
  241. WREG32(mmVGA_HDP_CONTROL, tmp);
  242. /* disable VGA render */
  243. tmp = RREG32(mmVGA_RENDER_CONTROL);
  244. tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
  245. WREG32(mmVGA_RENDER_CONTROL, tmp);
  246. }
  247. /* Update configuration */
  248. WREG32(mmMC_VM_SYSTEM_APERTURE_LOW_ADDR,
  249. adev->mc.vram_start >> 12);
  250. WREG32(mmMC_VM_SYSTEM_APERTURE_HIGH_ADDR,
  251. adev->mc.vram_end >> 12);
  252. WREG32(mmMC_VM_SYSTEM_APERTURE_DEFAULT_ADDR,
  253. adev->vram_scratch.gpu_addr >> 12);
  254. WREG32(mmMC_VM_AGP_BASE, 0);
  255. WREG32(mmMC_VM_AGP_TOP, 0x0FFFFFFF);
  256. WREG32(mmMC_VM_AGP_BOT, 0x0FFFFFFF);
  257. if (gmc_v7_0_wait_for_idle((void *)adev)) {
  258. dev_warn(adev->dev, "Wait for MC idle timedout !\n");
  259. }
  260. WREG32(mmBIF_FB_EN, BIF_FB_EN__FB_READ_EN_MASK | BIF_FB_EN__FB_WRITE_EN_MASK);
  261. tmp = RREG32(mmHDP_MISC_CNTL);
  262. tmp = REG_SET_FIELD(tmp, HDP_MISC_CNTL, FLUSH_INVALIDATE_CACHE, 0);
  263. WREG32(mmHDP_MISC_CNTL, tmp);
  264. tmp = RREG32(mmHDP_HOST_PATH_CNTL);
  265. WREG32(mmHDP_HOST_PATH_CNTL, tmp);
  266. }
  267. /**
  268. * gmc_v7_0_mc_init - initialize the memory controller driver params
  269. *
  270. * @adev: amdgpu_device pointer
  271. *
  272. * Look up the amount of vram, vram width, and decide how to place
  273. * vram and gart within the GPU's physical address space (CIK).
  274. * Returns 0 for success.
  275. */
  276. static int gmc_v7_0_mc_init(struct amdgpu_device *adev)
  277. {
  278. int r;
  279. adev->mc.vram_width = amdgpu_atombios_get_vram_width(adev);
  280. if (!adev->mc.vram_width) {
  281. u32 tmp;
  282. int chansize, numchan;
  283. /* Get VRAM informations */
  284. tmp = RREG32(mmMC_ARB_RAMCFG);
  285. if (REG_GET_FIELD(tmp, MC_ARB_RAMCFG, CHANSIZE)) {
  286. chansize = 64;
  287. } else {
  288. chansize = 32;
  289. }
  290. tmp = RREG32(mmMC_SHARED_CHMAP);
  291. switch (REG_GET_FIELD(tmp, MC_SHARED_CHMAP, NOOFCHAN)) {
  292. case 0:
  293. default:
  294. numchan = 1;
  295. break;
  296. case 1:
  297. numchan = 2;
  298. break;
  299. case 2:
  300. numchan = 4;
  301. break;
  302. case 3:
  303. numchan = 8;
  304. break;
  305. case 4:
  306. numchan = 3;
  307. break;
  308. case 5:
  309. numchan = 6;
  310. break;
  311. case 6:
  312. numchan = 10;
  313. break;
  314. case 7:
  315. numchan = 12;
  316. break;
  317. case 8:
  318. numchan = 16;
  319. break;
  320. }
  321. adev->mc.vram_width = numchan * chansize;
  322. }
  323. /* size in MB on si */
  324. adev->mc.mc_vram_size = RREG32(mmCONFIG_MEMSIZE) * 1024ULL * 1024ULL;
  325. adev->mc.real_vram_size = RREG32(mmCONFIG_MEMSIZE) * 1024ULL * 1024ULL;
  326. if (!(adev->flags & AMD_IS_APU)) {
  327. r = amdgpu_device_resize_fb_bar(adev);
  328. if (r)
  329. return r;
  330. }
  331. adev->mc.aper_base = pci_resource_start(adev->pdev, 0);
  332. adev->mc.aper_size = pci_resource_len(adev->pdev, 0);
  333. #ifdef CONFIG_X86_64
  334. if (adev->flags & AMD_IS_APU) {
  335. adev->mc.aper_base = ((u64)RREG32(mmMC_VM_FB_OFFSET)) << 22;
  336. adev->mc.aper_size = adev->mc.real_vram_size;
  337. }
  338. #endif
  339. /* In case the PCI BAR is larger than the actual amount of vram */
  340. adev->mc.visible_vram_size = adev->mc.aper_size;
  341. if (adev->mc.visible_vram_size > adev->mc.real_vram_size)
  342. adev->mc.visible_vram_size = adev->mc.real_vram_size;
  343. /* set the gart size */
  344. if (amdgpu_gart_size == -1) {
  345. switch (adev->asic_type) {
  346. case CHIP_TOPAZ: /* no MM engines */
  347. default:
  348. adev->mc.gart_size = 256ULL << 20;
  349. break;
  350. #ifdef CONFIG_DRM_AMDGPU_CIK
  351. case CHIP_BONAIRE: /* UVD, VCE do not support GPUVM */
  352. case CHIP_HAWAII: /* UVD, VCE do not support GPUVM */
  353. case CHIP_KAVERI: /* UVD, VCE do not support GPUVM */
  354. case CHIP_KABINI: /* UVD, VCE do not support GPUVM */
  355. case CHIP_MULLINS: /* UVD, VCE do not support GPUVM */
  356. adev->mc.gart_size = 1024ULL << 20;
  357. break;
  358. #endif
  359. }
  360. } else {
  361. adev->mc.gart_size = (u64)amdgpu_gart_size << 20;
  362. }
  363. gmc_v7_0_vram_gtt_location(adev, &adev->mc);
  364. return 0;
  365. }
  366. /*
  367. * GART
  368. * VMID 0 is the physical GPU addresses as used by the kernel.
  369. * VMIDs 1-15 are used for userspace clients and are handled
  370. * by the amdgpu vm/hsa code.
  371. */
  372. /**
  373. * gmc_v7_0_gart_flush_gpu_tlb - gart tlb flush callback
  374. *
  375. * @adev: amdgpu_device pointer
  376. * @vmid: vm instance to flush
  377. *
  378. * Flush the TLB for the requested page table (CIK).
  379. */
  380. static void gmc_v7_0_gart_flush_gpu_tlb(struct amdgpu_device *adev,
  381. uint32_t vmid)
  382. {
  383. /* flush hdp cache */
  384. WREG32(mmHDP_MEM_COHERENCY_FLUSH_CNTL, 0);
  385. /* bits 0-15 are the VM contexts0-15 */
  386. WREG32(mmVM_INVALIDATE_REQUEST, 1 << vmid);
  387. }
  388. /**
  389. * gmc_v7_0_gart_set_pte_pde - update the page tables using MMIO
  390. *
  391. * @adev: amdgpu_device pointer
  392. * @cpu_pt_addr: cpu address of the page table
  393. * @gpu_page_idx: entry in the page table to update
  394. * @addr: dst addr to write into pte/pde
  395. * @flags: access flags
  396. *
  397. * Update the page tables using the CPU.
  398. */
  399. static int gmc_v7_0_gart_set_pte_pde(struct amdgpu_device *adev,
  400. void *cpu_pt_addr,
  401. uint32_t gpu_page_idx,
  402. uint64_t addr,
  403. uint64_t flags)
  404. {
  405. void __iomem *ptr = (void *)cpu_pt_addr;
  406. uint64_t value;
  407. value = addr & 0xFFFFFFFFFFFFF000ULL;
  408. value |= flags;
  409. writeq(value, ptr + (gpu_page_idx * 8));
  410. return 0;
  411. }
  412. static uint64_t gmc_v7_0_get_vm_pte_flags(struct amdgpu_device *adev,
  413. uint32_t flags)
  414. {
  415. uint64_t pte_flag = 0;
  416. if (flags & AMDGPU_VM_PAGE_READABLE)
  417. pte_flag |= AMDGPU_PTE_READABLE;
  418. if (flags & AMDGPU_VM_PAGE_WRITEABLE)
  419. pte_flag |= AMDGPU_PTE_WRITEABLE;
  420. if (flags & AMDGPU_VM_PAGE_PRT)
  421. pte_flag |= AMDGPU_PTE_PRT;
  422. return pte_flag;
  423. }
  424. static uint64_t gmc_v7_0_get_vm_pde(struct amdgpu_device *adev, uint64_t addr)
  425. {
  426. BUG_ON(addr & 0xFFFFFF0000000FFFULL);
  427. return addr;
  428. }
  429. /**
  430. * gmc_v8_0_set_fault_enable_default - update VM fault handling
  431. *
  432. * @adev: amdgpu_device pointer
  433. * @value: true redirects VM faults to the default page
  434. */
  435. static void gmc_v7_0_set_fault_enable_default(struct amdgpu_device *adev,
  436. bool value)
  437. {
  438. u32 tmp;
  439. tmp = RREG32(mmVM_CONTEXT1_CNTL);
  440. tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
  441. RANGE_PROTECTION_FAULT_ENABLE_DEFAULT, value);
  442. tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
  443. DUMMY_PAGE_PROTECTION_FAULT_ENABLE_DEFAULT, value);
  444. tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
  445. PDE0_PROTECTION_FAULT_ENABLE_DEFAULT, value);
  446. tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
  447. VALID_PROTECTION_FAULT_ENABLE_DEFAULT, value);
  448. tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
  449. READ_PROTECTION_FAULT_ENABLE_DEFAULT, value);
  450. tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL,
  451. WRITE_PROTECTION_FAULT_ENABLE_DEFAULT, value);
  452. WREG32(mmVM_CONTEXT1_CNTL, tmp);
  453. }
  454. /**
  455. * gmc_v7_0_set_prt - set PRT VM fault
  456. *
  457. * @adev: amdgpu_device pointer
  458. * @enable: enable/disable VM fault handling for PRT
  459. */
  460. static void gmc_v7_0_set_prt(struct amdgpu_device *adev, bool enable)
  461. {
  462. uint32_t tmp;
  463. if (enable && !adev->mc.prt_warning) {
  464. dev_warn(adev->dev, "Disabling VM faults because of PRT request!\n");
  465. adev->mc.prt_warning = true;
  466. }
  467. tmp = RREG32(mmVM_PRT_CNTL);
  468. tmp = REG_SET_FIELD(tmp, VM_PRT_CNTL,
  469. CB_DISABLE_READ_FAULT_ON_UNMAPPED_ACCESS, enable);
  470. tmp = REG_SET_FIELD(tmp, VM_PRT_CNTL,
  471. CB_DISABLE_WRITE_FAULT_ON_UNMAPPED_ACCESS, enable);
  472. tmp = REG_SET_FIELD(tmp, VM_PRT_CNTL,
  473. TC_DISABLE_READ_FAULT_ON_UNMAPPED_ACCESS, enable);
  474. tmp = REG_SET_FIELD(tmp, VM_PRT_CNTL,
  475. TC_DISABLE_WRITE_FAULT_ON_UNMAPPED_ACCESS, enable);
  476. tmp = REG_SET_FIELD(tmp, VM_PRT_CNTL,
  477. L2_CACHE_STORE_INVALID_ENTRIES, enable);
  478. tmp = REG_SET_FIELD(tmp, VM_PRT_CNTL,
  479. L1_TLB_STORE_INVALID_ENTRIES, enable);
  480. tmp = REG_SET_FIELD(tmp, VM_PRT_CNTL,
  481. MASK_PDE0_FAULT, enable);
  482. WREG32(mmVM_PRT_CNTL, tmp);
  483. if (enable) {
  484. uint32_t low = AMDGPU_VA_RESERVED_SIZE >> AMDGPU_GPU_PAGE_SHIFT;
  485. uint32_t high = adev->vm_manager.max_pfn;
  486. WREG32(mmVM_PRT_APERTURE0_LOW_ADDR, low);
  487. WREG32(mmVM_PRT_APERTURE1_LOW_ADDR, low);
  488. WREG32(mmVM_PRT_APERTURE2_LOW_ADDR, low);
  489. WREG32(mmVM_PRT_APERTURE3_LOW_ADDR, low);
  490. WREG32(mmVM_PRT_APERTURE0_HIGH_ADDR, high);
  491. WREG32(mmVM_PRT_APERTURE1_HIGH_ADDR, high);
  492. WREG32(mmVM_PRT_APERTURE2_HIGH_ADDR, high);
  493. WREG32(mmVM_PRT_APERTURE3_HIGH_ADDR, high);
  494. } else {
  495. WREG32(mmVM_PRT_APERTURE0_LOW_ADDR, 0xfffffff);
  496. WREG32(mmVM_PRT_APERTURE1_LOW_ADDR, 0xfffffff);
  497. WREG32(mmVM_PRT_APERTURE2_LOW_ADDR, 0xfffffff);
  498. WREG32(mmVM_PRT_APERTURE3_LOW_ADDR, 0xfffffff);
  499. WREG32(mmVM_PRT_APERTURE0_HIGH_ADDR, 0x0);
  500. WREG32(mmVM_PRT_APERTURE1_HIGH_ADDR, 0x0);
  501. WREG32(mmVM_PRT_APERTURE2_HIGH_ADDR, 0x0);
  502. WREG32(mmVM_PRT_APERTURE3_HIGH_ADDR, 0x0);
  503. }
  504. }
  505. /**
  506. * gmc_v7_0_gart_enable - gart enable
  507. *
  508. * @adev: amdgpu_device pointer
  509. *
  510. * This sets up the TLBs, programs the page tables for VMID0,
  511. * sets up the hw for VMIDs 1-15 which are allocated on
  512. * demand, and sets up the global locations for the LDS, GDS,
  513. * and GPUVM for FSA64 clients (CIK).
  514. * Returns 0 for success, errors for failure.
  515. */
  516. static int gmc_v7_0_gart_enable(struct amdgpu_device *adev)
  517. {
  518. int r, i;
  519. u32 tmp, field;
  520. if (adev->gart.robj == NULL) {
  521. dev_err(adev->dev, "No VRAM object for PCIE GART.\n");
  522. return -EINVAL;
  523. }
  524. r = amdgpu_gart_table_vram_pin(adev);
  525. if (r)
  526. return r;
  527. /* Setup TLB control */
  528. tmp = RREG32(mmMC_VM_MX_L1_TLB_CNTL);
  529. tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, ENABLE_L1_TLB, 1);
  530. tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, ENABLE_L1_FRAGMENT_PROCESSING, 1);
  531. tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, SYSTEM_ACCESS_MODE, 3);
  532. tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, ENABLE_ADVANCED_DRIVER_MODEL, 1);
  533. tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, SYSTEM_APERTURE_UNMAPPED_ACCESS, 0);
  534. WREG32(mmMC_VM_MX_L1_TLB_CNTL, tmp);
  535. /* Setup L2 cache */
  536. tmp = RREG32(mmVM_L2_CNTL);
  537. tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, ENABLE_L2_CACHE, 1);
  538. tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, ENABLE_L2_FRAGMENT_PROCESSING, 1);
  539. tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, ENABLE_L2_PTE_CACHE_LRU_UPDATE_BY_WRITE, 1);
  540. tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, ENABLE_L2_PDE0_CACHE_LRU_UPDATE_BY_WRITE, 1);
  541. tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, EFFECTIVE_L2_QUEUE_SIZE, 7);
  542. tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, CONTEXT1_IDENTITY_ACCESS_MODE, 1);
  543. tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, ENABLE_DEFAULT_PAGE_OUT_TO_SYSTEM_MEMORY, 1);
  544. WREG32(mmVM_L2_CNTL, tmp);
  545. tmp = REG_SET_FIELD(0, VM_L2_CNTL2, INVALIDATE_ALL_L1_TLBS, 1);
  546. tmp = REG_SET_FIELD(tmp, VM_L2_CNTL2, INVALIDATE_L2_CACHE, 1);
  547. WREG32(mmVM_L2_CNTL2, tmp);
  548. field = adev->vm_manager.fragment_size;
  549. tmp = RREG32(mmVM_L2_CNTL3);
  550. tmp = REG_SET_FIELD(tmp, VM_L2_CNTL3, L2_CACHE_BIGK_ASSOCIATIVITY, 1);
  551. tmp = REG_SET_FIELD(tmp, VM_L2_CNTL3, BANK_SELECT, field);
  552. tmp = REG_SET_FIELD(tmp, VM_L2_CNTL3, L2_CACHE_BIGK_FRAGMENT_SIZE, field);
  553. WREG32(mmVM_L2_CNTL3, tmp);
  554. /* setup context0 */
  555. WREG32(mmVM_CONTEXT0_PAGE_TABLE_START_ADDR, adev->mc.gart_start >> 12);
  556. WREG32(mmVM_CONTEXT0_PAGE_TABLE_END_ADDR, adev->mc.gart_end >> 12);
  557. WREG32(mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR, adev->gart.table_addr >> 12);
  558. WREG32(mmVM_CONTEXT0_PROTECTION_FAULT_DEFAULT_ADDR,
  559. (u32)(adev->dummy_page.addr >> 12));
  560. WREG32(mmVM_CONTEXT0_CNTL2, 0);
  561. tmp = RREG32(mmVM_CONTEXT0_CNTL);
  562. tmp = REG_SET_FIELD(tmp, VM_CONTEXT0_CNTL, ENABLE_CONTEXT, 1);
  563. tmp = REG_SET_FIELD(tmp, VM_CONTEXT0_CNTL, PAGE_TABLE_DEPTH, 0);
  564. tmp = REG_SET_FIELD(tmp, VM_CONTEXT0_CNTL, RANGE_PROTECTION_FAULT_ENABLE_DEFAULT, 1);
  565. WREG32(mmVM_CONTEXT0_CNTL, tmp);
  566. WREG32(0x575, 0);
  567. WREG32(0x576, 0);
  568. WREG32(0x577, 0);
  569. /* empty context1-15 */
  570. /* FIXME start with 4G, once using 2 level pt switch to full
  571. * vm size space
  572. */
  573. /* set vm size, must be a multiple of 4 */
  574. WREG32(mmVM_CONTEXT1_PAGE_TABLE_START_ADDR, 0);
  575. WREG32(mmVM_CONTEXT1_PAGE_TABLE_END_ADDR, adev->vm_manager.max_pfn - 1);
  576. for (i = 1; i < 16; i++) {
  577. if (i < 8)
  578. WREG32(mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR + i,
  579. adev->gart.table_addr >> 12);
  580. else
  581. WREG32(mmVM_CONTEXT8_PAGE_TABLE_BASE_ADDR + i - 8,
  582. adev->gart.table_addr >> 12);
  583. }
  584. /* enable context1-15 */
  585. WREG32(mmVM_CONTEXT1_PROTECTION_FAULT_DEFAULT_ADDR,
  586. (u32)(adev->dummy_page.addr >> 12));
  587. WREG32(mmVM_CONTEXT1_CNTL2, 4);
  588. tmp = RREG32(mmVM_CONTEXT1_CNTL);
  589. tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL, ENABLE_CONTEXT, 1);
  590. tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL, PAGE_TABLE_DEPTH, 1);
  591. tmp = REG_SET_FIELD(tmp, VM_CONTEXT1_CNTL, PAGE_TABLE_BLOCK_SIZE,
  592. adev->vm_manager.block_size - 9);
  593. WREG32(mmVM_CONTEXT1_CNTL, tmp);
  594. if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_ALWAYS)
  595. gmc_v7_0_set_fault_enable_default(adev, false);
  596. else
  597. gmc_v7_0_set_fault_enable_default(adev, true);
  598. if (adev->asic_type == CHIP_KAVERI) {
  599. tmp = RREG32(mmCHUB_CONTROL);
  600. tmp &= ~BYPASS_VM;
  601. WREG32(mmCHUB_CONTROL, tmp);
  602. }
  603. gmc_v7_0_gart_flush_gpu_tlb(adev, 0);
  604. DRM_INFO("PCIE GART of %uM enabled (table at 0x%016llX).\n",
  605. (unsigned)(adev->mc.gart_size >> 20),
  606. (unsigned long long)adev->gart.table_addr);
  607. adev->gart.ready = true;
  608. return 0;
  609. }
  610. static int gmc_v7_0_gart_init(struct amdgpu_device *adev)
  611. {
  612. int r;
  613. if (adev->gart.robj) {
  614. WARN(1, "R600 PCIE GART already initialized\n");
  615. return 0;
  616. }
  617. /* Initialize common gart structure */
  618. r = amdgpu_gart_init(adev);
  619. if (r)
  620. return r;
  621. adev->gart.table_size = adev->gart.num_gpu_pages * 8;
  622. adev->gart.gart_pte_flags = 0;
  623. return amdgpu_gart_table_vram_alloc(adev);
  624. }
  625. /**
  626. * gmc_v7_0_gart_disable - gart disable
  627. *
  628. * @adev: amdgpu_device pointer
  629. *
  630. * This disables all VM page table (CIK).
  631. */
  632. static void gmc_v7_0_gart_disable(struct amdgpu_device *adev)
  633. {
  634. u32 tmp;
  635. /* Disable all tables */
  636. WREG32(mmVM_CONTEXT0_CNTL, 0);
  637. WREG32(mmVM_CONTEXT1_CNTL, 0);
  638. /* Setup TLB control */
  639. tmp = RREG32(mmMC_VM_MX_L1_TLB_CNTL);
  640. tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, ENABLE_L1_TLB, 0);
  641. tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, ENABLE_L1_FRAGMENT_PROCESSING, 0);
  642. tmp = REG_SET_FIELD(tmp, MC_VM_MX_L1_TLB_CNTL, ENABLE_ADVANCED_DRIVER_MODEL, 0);
  643. WREG32(mmMC_VM_MX_L1_TLB_CNTL, tmp);
  644. /* Setup L2 cache */
  645. tmp = RREG32(mmVM_L2_CNTL);
  646. tmp = REG_SET_FIELD(tmp, VM_L2_CNTL, ENABLE_L2_CACHE, 0);
  647. WREG32(mmVM_L2_CNTL, tmp);
  648. WREG32(mmVM_L2_CNTL2, 0);
  649. amdgpu_gart_table_vram_unpin(adev);
  650. }
  651. /**
  652. * gmc_v7_0_gart_fini - vm fini callback
  653. *
  654. * @adev: amdgpu_device pointer
  655. *
  656. * Tears down the driver GART/VM setup (CIK).
  657. */
  658. static void gmc_v7_0_gart_fini(struct amdgpu_device *adev)
  659. {
  660. amdgpu_gart_table_vram_free(adev);
  661. amdgpu_gart_fini(adev);
  662. }
  663. /**
  664. * gmc_v7_0_vm_decode_fault - print human readable fault info
  665. *
  666. * @adev: amdgpu_device pointer
  667. * @status: VM_CONTEXT1_PROTECTION_FAULT_STATUS register value
  668. * @addr: VM_CONTEXT1_PROTECTION_FAULT_ADDR register value
  669. *
  670. * Print human readable fault information (CIK).
  671. */
  672. static void gmc_v7_0_vm_decode_fault(struct amdgpu_device *adev,
  673. u32 status, u32 addr, u32 mc_client)
  674. {
  675. u32 mc_id;
  676. u32 vmid = REG_GET_FIELD(status, VM_CONTEXT1_PROTECTION_FAULT_STATUS, VMID);
  677. u32 protections = REG_GET_FIELD(status, VM_CONTEXT1_PROTECTION_FAULT_STATUS,
  678. PROTECTIONS);
  679. char block[5] = { mc_client >> 24, (mc_client >> 16) & 0xff,
  680. (mc_client >> 8) & 0xff, mc_client & 0xff, 0 };
  681. mc_id = REG_GET_FIELD(status, VM_CONTEXT1_PROTECTION_FAULT_STATUS,
  682. MEMORY_CLIENT_ID);
  683. dev_err(adev->dev, "VM fault (0x%02x, vmid %d) at page %u, %s from '%s' (0x%08x) (%d)\n",
  684. protections, vmid, addr,
  685. REG_GET_FIELD(status, VM_CONTEXT1_PROTECTION_FAULT_STATUS,
  686. MEMORY_CLIENT_RW) ?
  687. "write" : "read", block, mc_client, mc_id);
  688. }
  689. static const u32 mc_cg_registers[] = {
  690. mmMC_HUB_MISC_HUB_CG,
  691. mmMC_HUB_MISC_SIP_CG,
  692. mmMC_HUB_MISC_VM_CG,
  693. mmMC_XPB_CLK_GAT,
  694. mmATC_MISC_CG,
  695. mmMC_CITF_MISC_WR_CG,
  696. mmMC_CITF_MISC_RD_CG,
  697. mmMC_CITF_MISC_VM_CG,
  698. mmVM_L2_CG,
  699. };
  700. static const u32 mc_cg_ls_en[] = {
  701. MC_HUB_MISC_HUB_CG__MEM_LS_ENABLE_MASK,
  702. MC_HUB_MISC_SIP_CG__MEM_LS_ENABLE_MASK,
  703. MC_HUB_MISC_VM_CG__MEM_LS_ENABLE_MASK,
  704. MC_XPB_CLK_GAT__MEM_LS_ENABLE_MASK,
  705. ATC_MISC_CG__MEM_LS_ENABLE_MASK,
  706. MC_CITF_MISC_WR_CG__MEM_LS_ENABLE_MASK,
  707. MC_CITF_MISC_RD_CG__MEM_LS_ENABLE_MASK,
  708. MC_CITF_MISC_VM_CG__MEM_LS_ENABLE_MASK,
  709. VM_L2_CG__MEM_LS_ENABLE_MASK,
  710. };
  711. static const u32 mc_cg_en[] = {
  712. MC_HUB_MISC_HUB_CG__ENABLE_MASK,
  713. MC_HUB_MISC_SIP_CG__ENABLE_MASK,
  714. MC_HUB_MISC_VM_CG__ENABLE_MASK,
  715. MC_XPB_CLK_GAT__ENABLE_MASK,
  716. ATC_MISC_CG__ENABLE_MASK,
  717. MC_CITF_MISC_WR_CG__ENABLE_MASK,
  718. MC_CITF_MISC_RD_CG__ENABLE_MASK,
  719. MC_CITF_MISC_VM_CG__ENABLE_MASK,
  720. VM_L2_CG__ENABLE_MASK,
  721. };
  722. static void gmc_v7_0_enable_mc_ls(struct amdgpu_device *adev,
  723. bool enable)
  724. {
  725. int i;
  726. u32 orig, data;
  727. for (i = 0; i < ARRAY_SIZE(mc_cg_registers); i++) {
  728. orig = data = RREG32(mc_cg_registers[i]);
  729. if (enable && (adev->cg_flags & AMD_CG_SUPPORT_MC_LS))
  730. data |= mc_cg_ls_en[i];
  731. else
  732. data &= ~mc_cg_ls_en[i];
  733. if (data != orig)
  734. WREG32(mc_cg_registers[i], data);
  735. }
  736. }
  737. static void gmc_v7_0_enable_mc_mgcg(struct amdgpu_device *adev,
  738. bool enable)
  739. {
  740. int i;
  741. u32 orig, data;
  742. for (i = 0; i < ARRAY_SIZE(mc_cg_registers); i++) {
  743. orig = data = RREG32(mc_cg_registers[i]);
  744. if (enable && (adev->cg_flags & AMD_CG_SUPPORT_MC_MGCG))
  745. data |= mc_cg_en[i];
  746. else
  747. data &= ~mc_cg_en[i];
  748. if (data != orig)
  749. WREG32(mc_cg_registers[i], data);
  750. }
  751. }
  752. static void gmc_v7_0_enable_bif_mgls(struct amdgpu_device *adev,
  753. bool enable)
  754. {
  755. u32 orig, data;
  756. orig = data = RREG32_PCIE(ixPCIE_CNTL2);
  757. if (enable && (adev->cg_flags & AMD_CG_SUPPORT_BIF_LS)) {
  758. data = REG_SET_FIELD(data, PCIE_CNTL2, SLV_MEM_LS_EN, 1);
  759. data = REG_SET_FIELD(data, PCIE_CNTL2, MST_MEM_LS_EN, 1);
  760. data = REG_SET_FIELD(data, PCIE_CNTL2, REPLAY_MEM_LS_EN, 1);
  761. data = REG_SET_FIELD(data, PCIE_CNTL2, SLV_MEM_AGGRESSIVE_LS_EN, 1);
  762. } else {
  763. data = REG_SET_FIELD(data, PCIE_CNTL2, SLV_MEM_LS_EN, 0);
  764. data = REG_SET_FIELD(data, PCIE_CNTL2, MST_MEM_LS_EN, 0);
  765. data = REG_SET_FIELD(data, PCIE_CNTL2, REPLAY_MEM_LS_EN, 0);
  766. data = REG_SET_FIELD(data, PCIE_CNTL2, SLV_MEM_AGGRESSIVE_LS_EN, 0);
  767. }
  768. if (orig != data)
  769. WREG32_PCIE(ixPCIE_CNTL2, data);
  770. }
  771. static void gmc_v7_0_enable_hdp_mgcg(struct amdgpu_device *adev,
  772. bool enable)
  773. {
  774. u32 orig, data;
  775. orig = data = RREG32(mmHDP_HOST_PATH_CNTL);
  776. if (enable && (adev->cg_flags & AMD_CG_SUPPORT_HDP_MGCG))
  777. data = REG_SET_FIELD(data, HDP_HOST_PATH_CNTL, CLOCK_GATING_DIS, 0);
  778. else
  779. data = REG_SET_FIELD(data, HDP_HOST_PATH_CNTL, CLOCK_GATING_DIS, 1);
  780. if (orig != data)
  781. WREG32(mmHDP_HOST_PATH_CNTL, data);
  782. }
  783. static void gmc_v7_0_enable_hdp_ls(struct amdgpu_device *adev,
  784. bool enable)
  785. {
  786. u32 orig, data;
  787. orig = data = RREG32(mmHDP_MEM_POWER_LS);
  788. if (enable && (adev->cg_flags & AMD_CG_SUPPORT_HDP_LS))
  789. data = REG_SET_FIELD(data, HDP_MEM_POWER_LS, LS_ENABLE, 1);
  790. else
  791. data = REG_SET_FIELD(data, HDP_MEM_POWER_LS, LS_ENABLE, 0);
  792. if (orig != data)
  793. WREG32(mmHDP_MEM_POWER_LS, data);
  794. }
  795. static int gmc_v7_0_convert_vram_type(int mc_seq_vram_type)
  796. {
  797. switch (mc_seq_vram_type) {
  798. case MC_SEQ_MISC0__MT__GDDR1:
  799. return AMDGPU_VRAM_TYPE_GDDR1;
  800. case MC_SEQ_MISC0__MT__DDR2:
  801. return AMDGPU_VRAM_TYPE_DDR2;
  802. case MC_SEQ_MISC0__MT__GDDR3:
  803. return AMDGPU_VRAM_TYPE_GDDR3;
  804. case MC_SEQ_MISC0__MT__GDDR4:
  805. return AMDGPU_VRAM_TYPE_GDDR4;
  806. case MC_SEQ_MISC0__MT__GDDR5:
  807. return AMDGPU_VRAM_TYPE_GDDR5;
  808. case MC_SEQ_MISC0__MT__HBM:
  809. return AMDGPU_VRAM_TYPE_HBM;
  810. case MC_SEQ_MISC0__MT__DDR3:
  811. return AMDGPU_VRAM_TYPE_DDR3;
  812. default:
  813. return AMDGPU_VRAM_TYPE_UNKNOWN;
  814. }
  815. }
  816. static int gmc_v7_0_early_init(void *handle)
  817. {
  818. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  819. gmc_v7_0_set_gart_funcs(adev);
  820. gmc_v7_0_set_irq_funcs(adev);
  821. adev->mc.shared_aperture_start = 0x2000000000000000ULL;
  822. adev->mc.shared_aperture_end =
  823. adev->mc.shared_aperture_start + (4ULL << 30) - 1;
  824. adev->mc.private_aperture_start =
  825. adev->mc.shared_aperture_end + 1;
  826. adev->mc.private_aperture_end =
  827. adev->mc.private_aperture_start + (4ULL << 30) - 1;
  828. return 0;
  829. }
  830. static int gmc_v7_0_late_init(void *handle)
  831. {
  832. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  833. if (amdgpu_vm_fault_stop != AMDGPU_VM_FAULT_STOP_ALWAYS)
  834. return amdgpu_irq_get(adev, &adev->mc.vm_fault, 0);
  835. else
  836. return 0;
  837. }
  838. static int gmc_v7_0_sw_init(void *handle)
  839. {
  840. int r;
  841. int dma_bits;
  842. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  843. if (adev->flags & AMD_IS_APU) {
  844. adev->mc.vram_type = AMDGPU_VRAM_TYPE_UNKNOWN;
  845. } else {
  846. u32 tmp = RREG32(mmMC_SEQ_MISC0);
  847. tmp &= MC_SEQ_MISC0__MT__MASK;
  848. adev->mc.vram_type = gmc_v7_0_convert_vram_type(tmp);
  849. }
  850. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, 146, &adev->mc.vm_fault);
  851. if (r)
  852. return r;
  853. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, 147, &adev->mc.vm_fault);
  854. if (r)
  855. return r;
  856. /* Adjust VM size here.
  857. * Currently set to 4GB ((1 << 20) 4k pages).
  858. * Max GPUVM size for cayman and SI is 40 bits.
  859. */
  860. amdgpu_vm_adjust_size(adev, 64, 9);
  861. /* Set the internal MC address mask
  862. * This is the max address of the GPU's
  863. * internal address space.
  864. */
  865. adev->mc.mc_mask = 0xffffffffffULL; /* 40 bit MC */
  866. adev->mc.stolen_size = 256 * 1024;
  867. /* set DMA mask + need_dma32 flags.
  868. * PCIE - can handle 40-bits.
  869. * IGP - can handle 40-bits
  870. * PCI - dma32 for legacy pci gart, 40 bits on newer asics
  871. */
  872. adev->need_dma32 = false;
  873. dma_bits = adev->need_dma32 ? 32 : 40;
  874. r = pci_set_dma_mask(adev->pdev, DMA_BIT_MASK(dma_bits));
  875. if (r) {
  876. adev->need_dma32 = true;
  877. dma_bits = 32;
  878. pr_warn("amdgpu: No suitable DMA available\n");
  879. }
  880. r = pci_set_consistent_dma_mask(adev->pdev, DMA_BIT_MASK(dma_bits));
  881. if (r) {
  882. pci_set_consistent_dma_mask(adev->pdev, DMA_BIT_MASK(32));
  883. pr_warn("amdgpu: No coherent DMA available\n");
  884. }
  885. r = gmc_v7_0_init_microcode(adev);
  886. if (r) {
  887. DRM_ERROR("Failed to load mc firmware!\n");
  888. return r;
  889. }
  890. r = gmc_v7_0_mc_init(adev);
  891. if (r)
  892. return r;
  893. /* Memory manager */
  894. r = amdgpu_bo_init(adev);
  895. if (r)
  896. return r;
  897. r = gmc_v7_0_gart_init(adev);
  898. if (r)
  899. return r;
  900. /*
  901. * number of VMs
  902. * VMID 0 is reserved for System
  903. * amdgpu graphics/compute will use VMIDs 1-7
  904. * amdkfd will use VMIDs 8-15
  905. */
  906. adev->vm_manager.id_mgr[0].num_ids = AMDGPU_NUM_OF_VMIDS;
  907. adev->vm_manager.num_level = 1;
  908. amdgpu_vm_manager_init(adev);
  909. /* base offset of vram pages */
  910. if (adev->flags & AMD_IS_APU) {
  911. u64 tmp = RREG32(mmMC_VM_FB_OFFSET);
  912. tmp <<= 22;
  913. adev->vm_manager.vram_base_offset = tmp;
  914. } else {
  915. adev->vm_manager.vram_base_offset = 0;
  916. }
  917. return 0;
  918. }
  919. static int gmc_v7_0_sw_fini(void *handle)
  920. {
  921. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  922. amdgpu_gem_force_release(adev);
  923. amdgpu_vm_manager_fini(adev);
  924. gmc_v7_0_gart_fini(adev);
  925. amdgpu_bo_fini(adev);
  926. release_firmware(adev->mc.fw);
  927. adev->mc.fw = NULL;
  928. return 0;
  929. }
  930. static int gmc_v7_0_hw_init(void *handle)
  931. {
  932. int r;
  933. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  934. gmc_v7_0_init_golden_registers(adev);
  935. gmc_v7_0_mc_program(adev);
  936. if (!(adev->flags & AMD_IS_APU)) {
  937. r = gmc_v7_0_mc_load_microcode(adev);
  938. if (r) {
  939. DRM_ERROR("Failed to load MC firmware!\n");
  940. return r;
  941. }
  942. }
  943. r = gmc_v7_0_gart_enable(adev);
  944. if (r)
  945. return r;
  946. return r;
  947. }
  948. static int gmc_v7_0_hw_fini(void *handle)
  949. {
  950. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  951. amdgpu_irq_put(adev, &adev->mc.vm_fault, 0);
  952. gmc_v7_0_gart_disable(adev);
  953. return 0;
  954. }
  955. static int gmc_v7_0_suspend(void *handle)
  956. {
  957. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  958. gmc_v7_0_hw_fini(adev);
  959. return 0;
  960. }
  961. static int gmc_v7_0_resume(void *handle)
  962. {
  963. int r;
  964. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  965. r = gmc_v7_0_hw_init(adev);
  966. if (r)
  967. return r;
  968. amdgpu_vm_reset_all_ids(adev);
  969. return 0;
  970. }
  971. static bool gmc_v7_0_is_idle(void *handle)
  972. {
  973. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  974. u32 tmp = RREG32(mmSRBM_STATUS);
  975. if (tmp & (SRBM_STATUS__MCB_BUSY_MASK | SRBM_STATUS__MCB_NON_DISPLAY_BUSY_MASK |
  976. SRBM_STATUS__MCC_BUSY_MASK | SRBM_STATUS__MCD_BUSY_MASK | SRBM_STATUS__VMC_BUSY_MASK))
  977. return false;
  978. return true;
  979. }
  980. static int gmc_v7_0_wait_for_idle(void *handle)
  981. {
  982. unsigned i;
  983. u32 tmp;
  984. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  985. for (i = 0; i < adev->usec_timeout; i++) {
  986. /* read MC_STATUS */
  987. tmp = RREG32(mmSRBM_STATUS) & (SRBM_STATUS__MCB_BUSY_MASK |
  988. SRBM_STATUS__MCB_NON_DISPLAY_BUSY_MASK |
  989. SRBM_STATUS__MCC_BUSY_MASK |
  990. SRBM_STATUS__MCD_BUSY_MASK |
  991. SRBM_STATUS__VMC_BUSY_MASK);
  992. if (!tmp)
  993. return 0;
  994. udelay(1);
  995. }
  996. return -ETIMEDOUT;
  997. }
  998. static int gmc_v7_0_soft_reset(void *handle)
  999. {
  1000. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  1001. u32 srbm_soft_reset = 0;
  1002. u32 tmp = RREG32(mmSRBM_STATUS);
  1003. if (tmp & SRBM_STATUS__VMC_BUSY_MASK)
  1004. srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset,
  1005. SRBM_SOFT_RESET, SOFT_RESET_VMC, 1);
  1006. if (tmp & (SRBM_STATUS__MCB_BUSY_MASK | SRBM_STATUS__MCB_NON_DISPLAY_BUSY_MASK |
  1007. SRBM_STATUS__MCC_BUSY_MASK | SRBM_STATUS__MCD_BUSY_MASK)) {
  1008. if (!(adev->flags & AMD_IS_APU))
  1009. srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset,
  1010. SRBM_SOFT_RESET, SOFT_RESET_MC, 1);
  1011. }
  1012. if (srbm_soft_reset) {
  1013. gmc_v7_0_mc_stop(adev);
  1014. if (gmc_v7_0_wait_for_idle((void *)adev)) {
  1015. dev_warn(adev->dev, "Wait for GMC idle timed out !\n");
  1016. }
  1017. tmp = RREG32(mmSRBM_SOFT_RESET);
  1018. tmp |= srbm_soft_reset;
  1019. dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
  1020. WREG32(mmSRBM_SOFT_RESET, tmp);
  1021. tmp = RREG32(mmSRBM_SOFT_RESET);
  1022. udelay(50);
  1023. tmp &= ~srbm_soft_reset;
  1024. WREG32(mmSRBM_SOFT_RESET, tmp);
  1025. tmp = RREG32(mmSRBM_SOFT_RESET);
  1026. /* Wait a little for things to settle down */
  1027. udelay(50);
  1028. gmc_v7_0_mc_resume(adev);
  1029. udelay(50);
  1030. }
  1031. return 0;
  1032. }
  1033. static int gmc_v7_0_vm_fault_interrupt_state(struct amdgpu_device *adev,
  1034. struct amdgpu_irq_src *src,
  1035. unsigned type,
  1036. enum amdgpu_interrupt_state state)
  1037. {
  1038. u32 tmp;
  1039. u32 bits = (VM_CONTEXT1_CNTL__RANGE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
  1040. VM_CONTEXT1_CNTL__DUMMY_PAGE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
  1041. VM_CONTEXT1_CNTL__PDE0_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
  1042. VM_CONTEXT1_CNTL__VALID_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
  1043. VM_CONTEXT1_CNTL__READ_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
  1044. VM_CONTEXT1_CNTL__WRITE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK);
  1045. switch (state) {
  1046. case AMDGPU_IRQ_STATE_DISABLE:
  1047. /* system context */
  1048. tmp = RREG32(mmVM_CONTEXT0_CNTL);
  1049. tmp &= ~bits;
  1050. WREG32(mmVM_CONTEXT0_CNTL, tmp);
  1051. /* VMs */
  1052. tmp = RREG32(mmVM_CONTEXT1_CNTL);
  1053. tmp &= ~bits;
  1054. WREG32(mmVM_CONTEXT1_CNTL, tmp);
  1055. break;
  1056. case AMDGPU_IRQ_STATE_ENABLE:
  1057. /* system context */
  1058. tmp = RREG32(mmVM_CONTEXT0_CNTL);
  1059. tmp |= bits;
  1060. WREG32(mmVM_CONTEXT0_CNTL, tmp);
  1061. /* VMs */
  1062. tmp = RREG32(mmVM_CONTEXT1_CNTL);
  1063. tmp |= bits;
  1064. WREG32(mmVM_CONTEXT1_CNTL, tmp);
  1065. break;
  1066. default:
  1067. break;
  1068. }
  1069. return 0;
  1070. }
  1071. static int gmc_v7_0_process_interrupt(struct amdgpu_device *adev,
  1072. struct amdgpu_irq_src *source,
  1073. struct amdgpu_iv_entry *entry)
  1074. {
  1075. u32 addr, status, mc_client;
  1076. addr = RREG32(mmVM_CONTEXT1_PROTECTION_FAULT_ADDR);
  1077. status = RREG32(mmVM_CONTEXT1_PROTECTION_FAULT_STATUS);
  1078. mc_client = RREG32(mmVM_CONTEXT1_PROTECTION_FAULT_MCCLIENT);
  1079. /* reset addr and status */
  1080. WREG32_P(mmVM_CONTEXT1_CNTL2, 1, ~1);
  1081. if (!addr && !status)
  1082. return 0;
  1083. if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_FIRST)
  1084. gmc_v7_0_set_fault_enable_default(adev, false);
  1085. if (printk_ratelimit()) {
  1086. dev_err(adev->dev, "GPU fault detected: %d 0x%08x\n",
  1087. entry->src_id, entry->src_data[0]);
  1088. dev_err(adev->dev, " VM_CONTEXT1_PROTECTION_FAULT_ADDR 0x%08X\n",
  1089. addr);
  1090. dev_err(adev->dev, " VM_CONTEXT1_PROTECTION_FAULT_STATUS 0x%08X\n",
  1091. status);
  1092. gmc_v7_0_vm_decode_fault(adev, status, addr, mc_client);
  1093. }
  1094. return 0;
  1095. }
  1096. static int gmc_v7_0_set_clockgating_state(void *handle,
  1097. enum amd_clockgating_state state)
  1098. {
  1099. bool gate = false;
  1100. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  1101. if (state == AMD_CG_STATE_GATE)
  1102. gate = true;
  1103. if (!(adev->flags & AMD_IS_APU)) {
  1104. gmc_v7_0_enable_mc_mgcg(adev, gate);
  1105. gmc_v7_0_enable_mc_ls(adev, gate);
  1106. }
  1107. gmc_v7_0_enable_bif_mgls(adev, gate);
  1108. gmc_v7_0_enable_hdp_mgcg(adev, gate);
  1109. gmc_v7_0_enable_hdp_ls(adev, gate);
  1110. return 0;
  1111. }
  1112. static int gmc_v7_0_set_powergating_state(void *handle,
  1113. enum amd_powergating_state state)
  1114. {
  1115. return 0;
  1116. }
  1117. static const struct amd_ip_funcs gmc_v7_0_ip_funcs = {
  1118. .name = "gmc_v7_0",
  1119. .early_init = gmc_v7_0_early_init,
  1120. .late_init = gmc_v7_0_late_init,
  1121. .sw_init = gmc_v7_0_sw_init,
  1122. .sw_fini = gmc_v7_0_sw_fini,
  1123. .hw_init = gmc_v7_0_hw_init,
  1124. .hw_fini = gmc_v7_0_hw_fini,
  1125. .suspend = gmc_v7_0_suspend,
  1126. .resume = gmc_v7_0_resume,
  1127. .is_idle = gmc_v7_0_is_idle,
  1128. .wait_for_idle = gmc_v7_0_wait_for_idle,
  1129. .soft_reset = gmc_v7_0_soft_reset,
  1130. .set_clockgating_state = gmc_v7_0_set_clockgating_state,
  1131. .set_powergating_state = gmc_v7_0_set_powergating_state,
  1132. };
  1133. static const struct amdgpu_gart_funcs gmc_v7_0_gart_funcs = {
  1134. .flush_gpu_tlb = gmc_v7_0_gart_flush_gpu_tlb,
  1135. .set_pte_pde = gmc_v7_0_gart_set_pte_pde,
  1136. .set_prt = gmc_v7_0_set_prt,
  1137. .get_vm_pte_flags = gmc_v7_0_get_vm_pte_flags,
  1138. .get_vm_pde = gmc_v7_0_get_vm_pde
  1139. };
  1140. static const struct amdgpu_irq_src_funcs gmc_v7_0_irq_funcs = {
  1141. .set = gmc_v7_0_vm_fault_interrupt_state,
  1142. .process = gmc_v7_0_process_interrupt,
  1143. };
  1144. static void gmc_v7_0_set_gart_funcs(struct amdgpu_device *adev)
  1145. {
  1146. if (adev->gart.gart_funcs == NULL)
  1147. adev->gart.gart_funcs = &gmc_v7_0_gart_funcs;
  1148. }
  1149. static void gmc_v7_0_set_irq_funcs(struct amdgpu_device *adev)
  1150. {
  1151. adev->mc.vm_fault.num_types = 1;
  1152. adev->mc.vm_fault.funcs = &gmc_v7_0_irq_funcs;
  1153. }
  1154. const struct amdgpu_ip_block_version gmc_v7_0_ip_block =
  1155. {
  1156. .type = AMD_IP_BLOCK_TYPE_GMC,
  1157. .major = 7,
  1158. .minor = 0,
  1159. .rev = 0,
  1160. .funcs = &gmc_v7_0_ip_funcs,
  1161. };
  1162. const struct amdgpu_ip_block_version gmc_v7_4_ip_block =
  1163. {
  1164. .type = AMD_IP_BLOCK_TYPE_GMC,
  1165. .major = 7,
  1166. .minor = 4,
  1167. .rev = 0,
  1168. .funcs = &gmc_v7_0_ip_funcs,
  1169. };