gmc_v8_0.c 38 KB

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