gmc_v7_0.c 39 KB

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