gmc_v9_0.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. /*
  2. * Copyright 2016 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 "amdgpu.h"
  25. #include "gmc_v9_0.h"
  26. #include "amdgpu_atomfirmware.h"
  27. #include "vega10/soc15ip.h"
  28. #include "vega10/HDP/hdp_4_0_offset.h"
  29. #include "vega10/HDP/hdp_4_0_sh_mask.h"
  30. #include "vega10/GC/gc_9_0_sh_mask.h"
  31. #include "vega10/DC/dce_12_0_offset.h"
  32. #include "vega10/DC/dce_12_0_sh_mask.h"
  33. #include "vega10/vega10_enum.h"
  34. #include "soc15_common.h"
  35. #include "nbio_v6_1.h"
  36. #include "nbio_v7_0.h"
  37. #include "gfxhub_v1_0.h"
  38. #include "mmhub_v1_0.h"
  39. #define mmDF_CS_AON0_DramBaseAddress0 0x0044
  40. #define mmDF_CS_AON0_DramBaseAddress0_BASE_IDX 0
  41. //DF_CS_AON0_DramBaseAddress0
  42. #define DF_CS_AON0_DramBaseAddress0__AddrRngVal__SHIFT 0x0
  43. #define DF_CS_AON0_DramBaseAddress0__LgcyMmioHoleEn__SHIFT 0x1
  44. #define DF_CS_AON0_DramBaseAddress0__IntLvNumChan__SHIFT 0x4
  45. #define DF_CS_AON0_DramBaseAddress0__IntLvAddrSel__SHIFT 0x8
  46. #define DF_CS_AON0_DramBaseAddress0__DramBaseAddr__SHIFT 0xc
  47. #define DF_CS_AON0_DramBaseAddress0__AddrRngVal_MASK 0x00000001L
  48. #define DF_CS_AON0_DramBaseAddress0__LgcyMmioHoleEn_MASK 0x00000002L
  49. #define DF_CS_AON0_DramBaseAddress0__IntLvNumChan_MASK 0x000000F0L
  50. #define DF_CS_AON0_DramBaseAddress0__IntLvAddrSel_MASK 0x00000700L
  51. #define DF_CS_AON0_DramBaseAddress0__DramBaseAddr_MASK 0xFFFFF000L
  52. /* XXX Move this macro to VEGA10 header file, which is like vid.h for VI.*/
  53. #define AMDGPU_NUM_OF_VMIDS 8
  54. static const u32 golden_settings_vega10_hdp[] =
  55. {
  56. 0xf64, 0x0fffffff, 0x00000000,
  57. 0xf65, 0x0fffffff, 0x00000000,
  58. 0xf66, 0x0fffffff, 0x00000000,
  59. 0xf67, 0x0fffffff, 0x00000000,
  60. 0xf68, 0x0fffffff, 0x00000000,
  61. 0xf6a, 0x0fffffff, 0x00000000,
  62. 0xf6b, 0x0fffffff, 0x00000000,
  63. 0xf6c, 0x0fffffff, 0x00000000,
  64. 0xf6d, 0x0fffffff, 0x00000000,
  65. 0xf6e, 0x0fffffff, 0x00000000,
  66. };
  67. static int gmc_v9_0_vm_fault_interrupt_state(struct amdgpu_device *adev,
  68. struct amdgpu_irq_src *src,
  69. unsigned type,
  70. enum amdgpu_interrupt_state state)
  71. {
  72. struct amdgpu_vmhub *hub;
  73. u32 tmp, reg, bits, i;
  74. bits = VM_CONTEXT1_CNTL__RANGE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
  75. VM_CONTEXT1_CNTL__DUMMY_PAGE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
  76. VM_CONTEXT1_CNTL__PDE0_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
  77. VM_CONTEXT1_CNTL__VALID_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
  78. VM_CONTEXT1_CNTL__READ_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
  79. VM_CONTEXT1_CNTL__WRITE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
  80. VM_CONTEXT1_CNTL__EXECUTE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK;
  81. switch (state) {
  82. case AMDGPU_IRQ_STATE_DISABLE:
  83. /* MM HUB */
  84. hub = &adev->vmhub[AMDGPU_MMHUB];
  85. for (i = 0; i< 16; i++) {
  86. reg = hub->vm_context0_cntl + i;
  87. tmp = RREG32(reg);
  88. tmp &= ~bits;
  89. WREG32(reg, tmp);
  90. }
  91. /* GFX HUB */
  92. hub = &adev->vmhub[AMDGPU_GFXHUB];
  93. for (i = 0; i < 16; i++) {
  94. reg = hub->vm_context0_cntl + i;
  95. tmp = RREG32(reg);
  96. tmp &= ~bits;
  97. WREG32(reg, tmp);
  98. }
  99. break;
  100. case AMDGPU_IRQ_STATE_ENABLE:
  101. /* MM HUB */
  102. hub = &adev->vmhub[AMDGPU_MMHUB];
  103. for (i = 0; i< 16; i++) {
  104. reg = hub->vm_context0_cntl + i;
  105. tmp = RREG32(reg);
  106. tmp |= bits;
  107. WREG32(reg, tmp);
  108. }
  109. /* GFX HUB */
  110. hub = &adev->vmhub[AMDGPU_GFXHUB];
  111. for (i = 0; i < 16; i++) {
  112. reg = hub->vm_context0_cntl + i;
  113. tmp = RREG32(reg);
  114. tmp |= bits;
  115. WREG32(reg, tmp);
  116. }
  117. break;
  118. default:
  119. break;
  120. }
  121. return 0;
  122. }
  123. static int gmc_v9_0_process_interrupt(struct amdgpu_device *adev,
  124. struct amdgpu_irq_src *source,
  125. struct amdgpu_iv_entry *entry)
  126. {
  127. struct amdgpu_vmhub *hub = &adev->vmhub[entry->vm_id_src];
  128. uint32_t status = 0;
  129. u64 addr;
  130. addr = (u64)entry->src_data[0] << 12;
  131. addr |= ((u64)entry->src_data[1] & 0xf) << 44;
  132. if (!amdgpu_sriov_vf(adev)) {
  133. status = RREG32(hub->vm_l2_pro_fault_status);
  134. WREG32_P(hub->vm_l2_pro_fault_cntl, 1, ~1);
  135. }
  136. if (printk_ratelimit()) {
  137. dev_err(adev->dev,
  138. "[%s] VMC page fault (src_id:%u ring:%u vm_id:%u pas_id:%u)\n",
  139. entry->vm_id_src ? "mmhub" : "gfxhub",
  140. entry->src_id, entry->ring_id, entry->vm_id,
  141. entry->pas_id);
  142. dev_err(adev->dev, " at page 0x%016llx from %d\n",
  143. addr, entry->client_id);
  144. if (!amdgpu_sriov_vf(adev))
  145. dev_err(adev->dev,
  146. "VM_L2_PROTECTION_FAULT_STATUS:0x%08X\n",
  147. status);
  148. }
  149. return 0;
  150. }
  151. static const struct amdgpu_irq_src_funcs gmc_v9_0_irq_funcs = {
  152. .set = gmc_v9_0_vm_fault_interrupt_state,
  153. .process = gmc_v9_0_process_interrupt,
  154. };
  155. static void gmc_v9_0_set_irq_funcs(struct amdgpu_device *adev)
  156. {
  157. adev->mc.vm_fault.num_types = 1;
  158. adev->mc.vm_fault.funcs = &gmc_v9_0_irq_funcs;
  159. }
  160. static uint32_t gmc_v9_0_get_invalidate_req(unsigned int vm_id)
  161. {
  162. u32 req = 0;
  163. /* invalidate using legacy mode on vm_id*/
  164. req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ,
  165. PER_VMID_INVALIDATE_REQ, 1 << vm_id);
  166. req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, FLUSH_TYPE, 0);
  167. req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PTES, 1);
  168. req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PDE0, 1);
  169. req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PDE1, 1);
  170. req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PDE2, 1);
  171. req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L1_PTES, 1);
  172. req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ,
  173. CLEAR_PROTECTION_FAULT_STATUS_ADDR, 0);
  174. return req;
  175. }
  176. /*
  177. * GART
  178. * VMID 0 is the physical GPU addresses as used by the kernel.
  179. * VMIDs 1-15 are used for userspace clients and are handled
  180. * by the amdgpu vm/hsa code.
  181. */
  182. /**
  183. * gmc_v9_0_gart_flush_gpu_tlb - gart tlb flush callback
  184. *
  185. * @adev: amdgpu_device pointer
  186. * @vmid: vm instance to flush
  187. *
  188. * Flush the TLB for the requested page table.
  189. */
  190. static void gmc_v9_0_gart_flush_gpu_tlb(struct amdgpu_device *adev,
  191. uint32_t vmid)
  192. {
  193. /* Use register 17 for GART */
  194. const unsigned eng = 17;
  195. unsigned i, j;
  196. /* flush hdp cache */
  197. if (adev->flags & AMD_IS_APU)
  198. nbio_v7_0_hdp_flush(adev);
  199. else
  200. nbio_v6_1_hdp_flush(adev);
  201. spin_lock(&adev->mc.invalidate_lock);
  202. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  203. struct amdgpu_vmhub *hub = &adev->vmhub[i];
  204. u32 tmp = gmc_v9_0_get_invalidate_req(vmid);
  205. WREG32_NO_KIQ(hub->vm_inv_eng0_req + eng, tmp);
  206. /* Busy wait for ACK.*/
  207. for (j = 0; j < 100; j++) {
  208. tmp = RREG32_NO_KIQ(hub->vm_inv_eng0_ack + eng);
  209. tmp &= 1 << vmid;
  210. if (tmp)
  211. break;
  212. cpu_relax();
  213. }
  214. if (j < 100)
  215. continue;
  216. /* Wait for ACK with a delay.*/
  217. for (j = 0; j < adev->usec_timeout; j++) {
  218. tmp = RREG32_NO_KIQ(hub->vm_inv_eng0_ack + eng);
  219. tmp &= 1 << vmid;
  220. if (tmp)
  221. break;
  222. udelay(1);
  223. }
  224. if (j < adev->usec_timeout)
  225. continue;
  226. DRM_ERROR("Timeout waiting for VM flush ACK!\n");
  227. }
  228. spin_unlock(&adev->mc.invalidate_lock);
  229. }
  230. /**
  231. * gmc_v9_0_gart_set_pte_pde - update the page tables using MMIO
  232. *
  233. * @adev: amdgpu_device pointer
  234. * @cpu_pt_addr: cpu address of the page table
  235. * @gpu_page_idx: entry in the page table to update
  236. * @addr: dst addr to write into pte/pde
  237. * @flags: access flags
  238. *
  239. * Update the page tables using the CPU.
  240. */
  241. static int gmc_v9_0_gart_set_pte_pde(struct amdgpu_device *adev,
  242. void *cpu_pt_addr,
  243. uint32_t gpu_page_idx,
  244. uint64_t addr,
  245. uint64_t flags)
  246. {
  247. void __iomem *ptr = (void *)cpu_pt_addr;
  248. uint64_t value;
  249. /*
  250. * PTE format on VEGA 10:
  251. * 63:59 reserved
  252. * 58:57 mtype
  253. * 56 F
  254. * 55 L
  255. * 54 P
  256. * 53 SW
  257. * 52 T
  258. * 50:48 reserved
  259. * 47:12 4k physical page base address
  260. * 11:7 fragment
  261. * 6 write
  262. * 5 read
  263. * 4 exe
  264. * 3 Z
  265. * 2 snooped
  266. * 1 system
  267. * 0 valid
  268. *
  269. * PDE format on VEGA 10:
  270. * 63:59 block fragment size
  271. * 58:55 reserved
  272. * 54 P
  273. * 53:48 reserved
  274. * 47:6 physical base address of PD or PTE
  275. * 5:3 reserved
  276. * 2 C
  277. * 1 system
  278. * 0 valid
  279. */
  280. /*
  281. * The following is for PTE only. GART does not have PDEs.
  282. */
  283. value = addr & 0x0000FFFFFFFFF000ULL;
  284. value |= flags;
  285. writeq(value, ptr + (gpu_page_idx * 8));
  286. return 0;
  287. }
  288. static uint64_t gmc_v9_0_get_vm_pte_flags(struct amdgpu_device *adev,
  289. uint32_t flags)
  290. {
  291. uint64_t pte_flag = 0;
  292. if (flags & AMDGPU_VM_PAGE_EXECUTABLE)
  293. pte_flag |= AMDGPU_PTE_EXECUTABLE;
  294. if (flags & AMDGPU_VM_PAGE_READABLE)
  295. pte_flag |= AMDGPU_PTE_READABLE;
  296. if (flags & AMDGPU_VM_PAGE_WRITEABLE)
  297. pte_flag |= AMDGPU_PTE_WRITEABLE;
  298. switch (flags & AMDGPU_VM_MTYPE_MASK) {
  299. case AMDGPU_VM_MTYPE_DEFAULT:
  300. pte_flag |= AMDGPU_PTE_MTYPE(MTYPE_NC);
  301. break;
  302. case AMDGPU_VM_MTYPE_NC:
  303. pte_flag |= AMDGPU_PTE_MTYPE(MTYPE_NC);
  304. break;
  305. case AMDGPU_VM_MTYPE_WC:
  306. pte_flag |= AMDGPU_PTE_MTYPE(MTYPE_WC);
  307. break;
  308. case AMDGPU_VM_MTYPE_CC:
  309. pte_flag |= AMDGPU_PTE_MTYPE(MTYPE_CC);
  310. break;
  311. case AMDGPU_VM_MTYPE_UC:
  312. pte_flag |= AMDGPU_PTE_MTYPE(MTYPE_UC);
  313. break;
  314. default:
  315. pte_flag |= AMDGPU_PTE_MTYPE(MTYPE_NC);
  316. break;
  317. }
  318. if (flags & AMDGPU_VM_PAGE_PRT)
  319. pte_flag |= AMDGPU_PTE_PRT;
  320. return pte_flag;
  321. }
  322. static u64 gmc_v9_0_get_vm_pde(struct amdgpu_device *adev, u64 addr)
  323. {
  324. addr = adev->vm_manager.vram_base_offset + addr - adev->mc.vram_start;
  325. BUG_ON(addr & 0xFFFF00000000003FULL);
  326. return addr;
  327. }
  328. static const struct amdgpu_gart_funcs gmc_v9_0_gart_funcs = {
  329. .flush_gpu_tlb = gmc_v9_0_gart_flush_gpu_tlb,
  330. .set_pte_pde = gmc_v9_0_gart_set_pte_pde,
  331. .get_invalidate_req = gmc_v9_0_get_invalidate_req,
  332. .get_vm_pte_flags = gmc_v9_0_get_vm_pte_flags,
  333. .get_vm_pde = gmc_v9_0_get_vm_pde
  334. };
  335. static void gmc_v9_0_set_gart_funcs(struct amdgpu_device *adev)
  336. {
  337. if (adev->gart.gart_funcs == NULL)
  338. adev->gart.gart_funcs = &gmc_v9_0_gart_funcs;
  339. }
  340. static int gmc_v9_0_early_init(void *handle)
  341. {
  342. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  343. gmc_v9_0_set_gart_funcs(adev);
  344. gmc_v9_0_set_irq_funcs(adev);
  345. return 0;
  346. }
  347. static int gmc_v9_0_late_init(void *handle)
  348. {
  349. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  350. /*
  351. * The latest engine allocation on gfx9 is:
  352. * Engine 0, 1: idle
  353. * Engine 2, 3: firmware
  354. * Engine 4~13: amdgpu ring, subject to change when ring number changes
  355. * Engine 14~15: idle
  356. * Engine 16: kfd tlb invalidation
  357. * Engine 17: Gart flushes
  358. */
  359. unsigned vm_inv_eng[AMDGPU_MAX_VMHUBS] = { 4, 4 };
  360. unsigned i;
  361. for(i = 0; i < adev->num_rings; ++i) {
  362. struct amdgpu_ring *ring = adev->rings[i];
  363. unsigned vmhub = ring->funcs->vmhub;
  364. ring->vm_inv_eng = vm_inv_eng[vmhub]++;
  365. dev_info(adev->dev, "ring %u(%s) uses VM inv eng %u on hub %u\n",
  366. ring->idx, ring->name, ring->vm_inv_eng,
  367. ring->funcs->vmhub);
  368. }
  369. /* Engine 16 is used for KFD and 17 for GART flushes */
  370. for(i = 0; i < AMDGPU_MAX_VMHUBS; ++i)
  371. BUG_ON(vm_inv_eng[i] > 16);
  372. return amdgpu_irq_get(adev, &adev->mc.vm_fault, 0);
  373. }
  374. static void gmc_v9_0_vram_gtt_location(struct amdgpu_device *adev,
  375. struct amdgpu_mc *mc)
  376. {
  377. u64 base = 0;
  378. if (!amdgpu_sriov_vf(adev))
  379. base = mmhub_v1_0_get_fb_location(adev);
  380. amdgpu_vram_location(adev, &adev->mc, base);
  381. amdgpu_gart_location(adev, mc);
  382. /* base offset of vram pages */
  383. if (adev->flags & AMD_IS_APU)
  384. adev->vm_manager.vram_base_offset = gfxhub_v1_0_get_mc_fb_offset(adev);
  385. else
  386. adev->vm_manager.vram_base_offset = 0;
  387. }
  388. /**
  389. * gmc_v9_0_mc_init - initialize the memory controller driver params
  390. *
  391. * @adev: amdgpu_device pointer
  392. *
  393. * Look up the amount of vram, vram width, and decide how to place
  394. * vram and gart within the GPU's physical address space.
  395. * Returns 0 for success.
  396. */
  397. static int gmc_v9_0_mc_init(struct amdgpu_device *adev)
  398. {
  399. u32 tmp;
  400. int chansize, numchan;
  401. adev->mc.vram_width = amdgpu_atomfirmware_get_vram_width(adev);
  402. if (!adev->mc.vram_width) {
  403. /* hbm memory channel size */
  404. if (adev->flags & AMD_IS_APU)
  405. chansize = 64;
  406. else
  407. chansize = 128;
  408. tmp = RREG32_SOC15(DF, 0, mmDF_CS_AON0_DramBaseAddress0);
  409. tmp &= DF_CS_AON0_DramBaseAddress0__IntLvNumChan_MASK;
  410. tmp >>= DF_CS_AON0_DramBaseAddress0__IntLvNumChan__SHIFT;
  411. switch (tmp) {
  412. case 0:
  413. default:
  414. numchan = 1;
  415. break;
  416. case 1:
  417. numchan = 2;
  418. break;
  419. case 2:
  420. numchan = 0;
  421. break;
  422. case 3:
  423. numchan = 4;
  424. break;
  425. case 4:
  426. numchan = 0;
  427. break;
  428. case 5:
  429. numchan = 8;
  430. break;
  431. case 6:
  432. numchan = 0;
  433. break;
  434. case 7:
  435. numchan = 16;
  436. break;
  437. case 8:
  438. numchan = 2;
  439. break;
  440. }
  441. adev->mc.vram_width = numchan * chansize;
  442. }
  443. /* Could aper size report 0 ? */
  444. adev->mc.aper_base = pci_resource_start(adev->pdev, 0);
  445. adev->mc.aper_size = pci_resource_len(adev->pdev, 0);
  446. /* size in MB on si */
  447. adev->mc.mc_vram_size =
  448. ((adev->flags & AMD_IS_APU) ? nbio_v7_0_get_memsize(adev) :
  449. nbio_v6_1_get_memsize(adev)) * 1024ULL * 1024ULL;
  450. adev->mc.real_vram_size = adev->mc.mc_vram_size;
  451. adev->mc.visible_vram_size = adev->mc.aper_size;
  452. /* In case the PCI BAR is larger than the actual amount of vram */
  453. if (adev->mc.visible_vram_size > adev->mc.real_vram_size)
  454. adev->mc.visible_vram_size = adev->mc.real_vram_size;
  455. /* set the gart size */
  456. if (amdgpu_gart_size == -1) {
  457. switch (adev->asic_type) {
  458. case CHIP_VEGA10: /* all engines support GPUVM */
  459. default:
  460. adev->mc.gart_size = 256ULL << 20;
  461. break;
  462. case CHIP_RAVEN: /* DCE SG support */
  463. adev->mc.gart_size = 1024ULL << 20;
  464. break;
  465. }
  466. } else {
  467. adev->mc.gart_size = (u64)amdgpu_gart_size << 20;
  468. }
  469. gmc_v9_0_vram_gtt_location(adev, &adev->mc);
  470. return 0;
  471. }
  472. static int gmc_v9_0_gart_init(struct amdgpu_device *adev)
  473. {
  474. int r;
  475. if (adev->gart.robj) {
  476. WARN(1, "VEGA10 PCIE GART already initialized\n");
  477. return 0;
  478. }
  479. /* Initialize common gart structure */
  480. r = amdgpu_gart_init(adev);
  481. if (r)
  482. return r;
  483. adev->gart.table_size = adev->gart.num_gpu_pages * 8;
  484. adev->gart.gart_pte_flags = AMDGPU_PTE_MTYPE(MTYPE_UC) |
  485. AMDGPU_PTE_EXECUTABLE;
  486. return amdgpu_gart_table_vram_alloc(adev);
  487. }
  488. static int gmc_v9_0_sw_init(void *handle)
  489. {
  490. int r;
  491. int dma_bits;
  492. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  493. gfxhub_v1_0_init(adev);
  494. mmhub_v1_0_init(adev);
  495. spin_lock_init(&adev->mc.invalidate_lock);
  496. switch (adev->asic_type) {
  497. case CHIP_RAVEN:
  498. adev->mc.vram_type = AMDGPU_VRAM_TYPE_UNKNOWN;
  499. if (adev->rev_id == 0x0 || adev->rev_id == 0x1) {
  500. adev->vm_manager.vm_size = 1U << 18;
  501. adev->vm_manager.block_size = 9;
  502. adev->vm_manager.num_level = 3;
  503. amdgpu_vm_set_fragment_size(adev, 9);
  504. } else {
  505. /* vm_size is 64GB for legacy 2-level page support */
  506. amdgpu_vm_adjust_size(adev, 64, 9);
  507. adev->vm_manager.num_level = 1;
  508. }
  509. break;
  510. case CHIP_VEGA10:
  511. /* XXX Don't know how to get VRAM type yet. */
  512. adev->mc.vram_type = AMDGPU_VRAM_TYPE_HBM;
  513. /*
  514. * To fulfill 4-level page support,
  515. * vm size is 256TB (48bit), maximum size of Vega10,
  516. * block size 512 (9bit)
  517. */
  518. adev->vm_manager.vm_size = 1U << 18;
  519. adev->vm_manager.block_size = 9;
  520. adev->vm_manager.num_level = 3;
  521. amdgpu_vm_set_fragment_size(adev, 9);
  522. break;
  523. default:
  524. break;
  525. }
  526. DRM_INFO("vm size is %llu GB, block size is %u-bit,fragment size is %u-bit\n",
  527. adev->vm_manager.vm_size,
  528. adev->vm_manager.block_size,
  529. adev->vm_manager.fragment_size);
  530. /* This interrupt is VMC page fault.*/
  531. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_VMC, 0,
  532. &adev->mc.vm_fault);
  533. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_UTCL2, 0,
  534. &adev->mc.vm_fault);
  535. if (r)
  536. return r;
  537. adev->vm_manager.max_pfn = adev->vm_manager.vm_size << 18;
  538. /* Set the internal MC address mask
  539. * This is the max address of the GPU's
  540. * internal address space.
  541. */
  542. adev->mc.mc_mask = 0xffffffffffffULL; /* 48 bit MC */
  543. /*
  544. * It needs to reserve 8M stolen memory for vega10
  545. * TODO: Figure out how to avoid that...
  546. */
  547. adev->mc.stolen_size = 8 * 1024 * 1024;
  548. /* set DMA mask + need_dma32 flags.
  549. * PCIE - can handle 44-bits.
  550. * IGP - can handle 44-bits
  551. * PCI - dma32 for legacy pci gart, 44 bits on vega10
  552. */
  553. adev->need_dma32 = false;
  554. dma_bits = adev->need_dma32 ? 32 : 44;
  555. r = pci_set_dma_mask(adev->pdev, DMA_BIT_MASK(dma_bits));
  556. if (r) {
  557. adev->need_dma32 = true;
  558. dma_bits = 32;
  559. printk(KERN_WARNING "amdgpu: No suitable DMA available.\n");
  560. }
  561. r = pci_set_consistent_dma_mask(adev->pdev, DMA_BIT_MASK(dma_bits));
  562. if (r) {
  563. pci_set_consistent_dma_mask(adev->pdev, DMA_BIT_MASK(32));
  564. printk(KERN_WARNING "amdgpu: No coherent DMA available.\n");
  565. }
  566. r = gmc_v9_0_mc_init(adev);
  567. if (r)
  568. return r;
  569. /* Memory manager */
  570. r = amdgpu_bo_init(adev);
  571. if (r)
  572. return r;
  573. r = gmc_v9_0_gart_init(adev);
  574. if (r)
  575. return r;
  576. /*
  577. * number of VMs
  578. * VMID 0 is reserved for System
  579. * amdgpu graphics/compute will use VMIDs 1-7
  580. * amdkfd will use VMIDs 8-15
  581. */
  582. adev->vm_manager.id_mgr[AMDGPU_GFXHUB].num_ids = AMDGPU_NUM_OF_VMIDS;
  583. adev->vm_manager.id_mgr[AMDGPU_MMHUB].num_ids = AMDGPU_NUM_OF_VMIDS;
  584. amdgpu_vm_manager_init(adev);
  585. return 0;
  586. }
  587. /**
  588. * gmc_v8_0_gart_fini - vm fini callback
  589. *
  590. * @adev: amdgpu_device pointer
  591. *
  592. * Tears down the driver GART/VM setup (CIK).
  593. */
  594. static void gmc_v9_0_gart_fini(struct amdgpu_device *adev)
  595. {
  596. amdgpu_gart_table_vram_free(adev);
  597. amdgpu_gart_fini(adev);
  598. }
  599. static int gmc_v9_0_sw_fini(void *handle)
  600. {
  601. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  602. amdgpu_vm_manager_fini(adev);
  603. gmc_v9_0_gart_fini(adev);
  604. amdgpu_gem_force_release(adev);
  605. amdgpu_bo_fini(adev);
  606. return 0;
  607. }
  608. static void gmc_v9_0_init_golden_registers(struct amdgpu_device *adev)
  609. {
  610. switch (adev->asic_type) {
  611. case CHIP_VEGA10:
  612. break;
  613. case CHIP_RAVEN:
  614. break;
  615. default:
  616. break;
  617. }
  618. }
  619. /**
  620. * gmc_v9_0_gart_enable - gart enable
  621. *
  622. * @adev: amdgpu_device pointer
  623. */
  624. static int gmc_v9_0_gart_enable(struct amdgpu_device *adev)
  625. {
  626. int r;
  627. bool value;
  628. u32 tmp;
  629. amdgpu_program_register_sequence(adev,
  630. golden_settings_vega10_hdp,
  631. (const u32)ARRAY_SIZE(golden_settings_vega10_hdp));
  632. if (adev->gart.robj == NULL) {
  633. dev_err(adev->dev, "No VRAM object for PCIE GART.\n");
  634. return -EINVAL;
  635. }
  636. r = amdgpu_gart_table_vram_pin(adev);
  637. if (r)
  638. return r;
  639. /* After HDP is initialized, flush HDP.*/
  640. if (adev->flags & AMD_IS_APU)
  641. nbio_v7_0_hdp_flush(adev);
  642. else
  643. nbio_v6_1_hdp_flush(adev);
  644. switch (adev->asic_type) {
  645. case CHIP_RAVEN:
  646. mmhub_v1_0_initialize_power_gating(adev);
  647. mmhub_v1_0_update_power_gating(adev, true);
  648. break;
  649. default:
  650. break;
  651. }
  652. r = gfxhub_v1_0_gart_enable(adev);
  653. if (r)
  654. return r;
  655. r = mmhub_v1_0_gart_enable(adev);
  656. if (r)
  657. return r;
  658. tmp = RREG32_SOC15(HDP, 0, mmHDP_MISC_CNTL);
  659. tmp |= HDP_MISC_CNTL__FLUSH_INVALIDATE_CACHE_MASK;
  660. WREG32_SOC15(HDP, 0, mmHDP_MISC_CNTL, tmp);
  661. tmp = RREG32_SOC15(HDP, 0, mmHDP_HOST_PATH_CNTL);
  662. WREG32_SOC15(HDP, 0, mmHDP_HOST_PATH_CNTL, tmp);
  663. if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_ALWAYS)
  664. value = false;
  665. else
  666. value = true;
  667. gfxhub_v1_0_set_fault_enable_default(adev, value);
  668. mmhub_v1_0_set_fault_enable_default(adev, value);
  669. gmc_v9_0_gart_flush_gpu_tlb(adev, 0);
  670. DRM_INFO("PCIE GART of %uM enabled (table at 0x%016llX).\n",
  671. (unsigned)(adev->mc.gart_size >> 20),
  672. (unsigned long long)adev->gart.table_addr);
  673. adev->gart.ready = true;
  674. return 0;
  675. }
  676. static int gmc_v9_0_hw_init(void *handle)
  677. {
  678. int r;
  679. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  680. /* The sequence of these two function calls matters.*/
  681. gmc_v9_0_init_golden_registers(adev);
  682. if (adev->mode_info.num_crtc) {
  683. u32 tmp;
  684. /* Lockout access through VGA aperture*/
  685. tmp = RREG32_SOC15(DCE, 0, mmVGA_HDP_CONTROL);
  686. tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
  687. WREG32_SOC15(DCE, 0, mmVGA_HDP_CONTROL, tmp);
  688. /* disable VGA render */
  689. tmp = RREG32_SOC15(DCE, 0, mmVGA_RENDER_CONTROL);
  690. tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
  691. WREG32_SOC15(DCE, 0, mmVGA_RENDER_CONTROL, tmp);
  692. }
  693. r = gmc_v9_0_gart_enable(adev);
  694. return r;
  695. }
  696. /**
  697. * gmc_v9_0_gart_disable - gart disable
  698. *
  699. * @adev: amdgpu_device pointer
  700. *
  701. * This disables all VM page table.
  702. */
  703. static void gmc_v9_0_gart_disable(struct amdgpu_device *adev)
  704. {
  705. gfxhub_v1_0_gart_disable(adev);
  706. mmhub_v1_0_gart_disable(adev);
  707. amdgpu_gart_table_vram_unpin(adev);
  708. }
  709. static int gmc_v9_0_hw_fini(void *handle)
  710. {
  711. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  712. if (amdgpu_sriov_vf(adev)) {
  713. /* full access mode, so don't touch any GMC register */
  714. DRM_DEBUG("For SRIOV client, shouldn't do anything.\n");
  715. return 0;
  716. }
  717. amdgpu_irq_put(adev, &adev->mc.vm_fault, 0);
  718. gmc_v9_0_gart_disable(adev);
  719. return 0;
  720. }
  721. static int gmc_v9_0_suspend(void *handle)
  722. {
  723. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  724. gmc_v9_0_hw_fini(adev);
  725. return 0;
  726. }
  727. static int gmc_v9_0_resume(void *handle)
  728. {
  729. int r;
  730. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  731. r = gmc_v9_0_hw_init(adev);
  732. if (r)
  733. return r;
  734. amdgpu_vm_reset_all_ids(adev);
  735. return 0;
  736. }
  737. static bool gmc_v9_0_is_idle(void *handle)
  738. {
  739. /* MC is always ready in GMC v9.*/
  740. return true;
  741. }
  742. static int gmc_v9_0_wait_for_idle(void *handle)
  743. {
  744. /* There is no need to wait for MC idle in GMC v9.*/
  745. return 0;
  746. }
  747. static int gmc_v9_0_soft_reset(void *handle)
  748. {
  749. /* XXX for emulation.*/
  750. return 0;
  751. }
  752. static int gmc_v9_0_set_clockgating_state(void *handle,
  753. enum amd_clockgating_state state)
  754. {
  755. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  756. return mmhub_v1_0_set_clockgating(adev, state);
  757. }
  758. static void gmc_v9_0_get_clockgating_state(void *handle, u32 *flags)
  759. {
  760. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  761. mmhub_v1_0_get_clockgating(adev, flags);
  762. }
  763. static int gmc_v9_0_set_powergating_state(void *handle,
  764. enum amd_powergating_state state)
  765. {
  766. return 0;
  767. }
  768. const struct amd_ip_funcs gmc_v9_0_ip_funcs = {
  769. .name = "gmc_v9_0",
  770. .early_init = gmc_v9_0_early_init,
  771. .late_init = gmc_v9_0_late_init,
  772. .sw_init = gmc_v9_0_sw_init,
  773. .sw_fini = gmc_v9_0_sw_fini,
  774. .hw_init = gmc_v9_0_hw_init,
  775. .hw_fini = gmc_v9_0_hw_fini,
  776. .suspend = gmc_v9_0_suspend,
  777. .resume = gmc_v9_0_resume,
  778. .is_idle = gmc_v9_0_is_idle,
  779. .wait_for_idle = gmc_v9_0_wait_for_idle,
  780. .soft_reset = gmc_v9_0_soft_reset,
  781. .set_clockgating_state = gmc_v9_0_set_clockgating_state,
  782. .set_powergating_state = gmc_v9_0_set_powergating_state,
  783. .get_clockgating_state = gmc_v9_0_get_clockgating_state,
  784. };
  785. const struct amdgpu_ip_block_version gmc_v9_0_ip_block =
  786. {
  787. .type = AMD_IP_BLOCK_TYPE_GMC,
  788. .major = 9,
  789. .minor = 0,
  790. .rev = 0,
  791. .funcs = &gmc_v9_0_ip_funcs,
  792. };