intel-gtt.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. /*
  2. * Intel GTT (Graphics Translation Table) routines
  3. *
  4. * Caveat: This driver implements the linux agp interface, but this is far from
  5. * a agp driver! GTT support ended up here for purely historical reasons: The
  6. * old userspace intel graphics drivers needed an interface to map memory into
  7. * the GTT. And the drm provides a default interface for graphic devices sitting
  8. * on an agp port. So it made sense to fake the GTT support as an agp port to
  9. * avoid having to create a new api.
  10. *
  11. * With gem this does not make much sense anymore, just needlessly complicates
  12. * the code. But as long as the old graphics stack is still support, it's stuck
  13. * here.
  14. *
  15. * /fairy-tale-mode off
  16. */
  17. #include <linux/module.h>
  18. #include <linux/pci.h>
  19. #include <linux/kernel.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/agp_backend.h>
  22. #include <linux/delay.h>
  23. #include <asm/smp.h>
  24. #include "agp.h"
  25. #include "intel-agp.h"
  26. #include <drm/intel-gtt.h>
  27. /*
  28. * If we have Intel graphics, we're not going to have anything other than
  29. * an Intel IOMMU. So make the correct use of the PCI DMA API contingent
  30. * on the Intel IOMMU support (CONFIG_INTEL_IOMMU).
  31. * Only newer chipsets need to bother with this, of course.
  32. */
  33. #ifdef CONFIG_INTEL_IOMMU
  34. #define USE_PCI_DMA_API 1
  35. #else
  36. #define USE_PCI_DMA_API 0
  37. #endif
  38. struct intel_gtt_driver {
  39. unsigned int gen : 8;
  40. unsigned int is_g33 : 1;
  41. unsigned int is_pineview : 1;
  42. unsigned int is_ironlake : 1;
  43. unsigned int has_pgtbl_enable : 1;
  44. unsigned int dma_mask_size : 8;
  45. /* Chipset specific GTT setup */
  46. int (*setup)(void);
  47. /* This should undo anything done in ->setup() save the unmapping
  48. * of the mmio register file, that's done in the generic code. */
  49. void (*cleanup)(void);
  50. void (*write_entry)(dma_addr_t addr, unsigned int entry, unsigned int flags);
  51. /* Flags is a more or less chipset specific opaque value.
  52. * For chipsets that need to support old ums (non-gem) code, this
  53. * needs to be identical to the various supported agp memory types! */
  54. bool (*check_flags)(unsigned int flags);
  55. void (*chipset_flush)(void);
  56. };
  57. static struct _intel_private {
  58. const struct intel_gtt_driver *driver;
  59. struct pci_dev *pcidev; /* device one */
  60. struct pci_dev *bridge_dev;
  61. u8 __iomem *registers;
  62. phys_addr_t gtt_phys_addr;
  63. u32 PGETBL_save;
  64. u32 __iomem *gtt; /* I915G */
  65. bool clear_fake_agp; /* on first access via agp, fill with scratch */
  66. int num_dcache_entries;
  67. void __iomem *i9xx_flush_page;
  68. char *i81x_gtt_table;
  69. struct resource ifp_resource;
  70. int resource_valid;
  71. struct page *scratch_page;
  72. phys_addr_t scratch_page_dma;
  73. int refcount;
  74. /* Whether i915 needs to use the dmar apis or not. */
  75. unsigned int needs_dmar : 1;
  76. phys_addr_t gma_bus_addr;
  77. /* Size of memory reserved for graphics by the BIOS */
  78. unsigned int stolen_size;
  79. /* Total number of gtt entries. */
  80. unsigned int gtt_total_entries;
  81. /* Part of the gtt that is mappable by the cpu, for those chips where
  82. * this is not the full gtt. */
  83. unsigned int gtt_mappable_entries;
  84. } intel_private;
  85. #define INTEL_GTT_GEN intel_private.driver->gen
  86. #define IS_G33 intel_private.driver->is_g33
  87. #define IS_PINEVIEW intel_private.driver->is_pineview
  88. #define IS_IRONLAKE intel_private.driver->is_ironlake
  89. #define HAS_PGTBL_EN intel_private.driver->has_pgtbl_enable
  90. #if IS_ENABLED(CONFIG_AGP_INTEL)
  91. static int intel_gtt_map_memory(struct page **pages,
  92. unsigned int num_entries,
  93. struct sg_table *st)
  94. {
  95. struct scatterlist *sg;
  96. int i;
  97. DBG("try mapping %lu pages\n", (unsigned long)num_entries);
  98. if (sg_alloc_table(st, num_entries, GFP_KERNEL))
  99. goto err;
  100. for_each_sg(st->sgl, sg, num_entries, i)
  101. sg_set_page(sg, pages[i], PAGE_SIZE, 0);
  102. if (!pci_map_sg(intel_private.pcidev,
  103. st->sgl, st->nents, PCI_DMA_BIDIRECTIONAL))
  104. goto err;
  105. return 0;
  106. err:
  107. sg_free_table(st);
  108. return -ENOMEM;
  109. }
  110. static void intel_gtt_unmap_memory(struct scatterlist *sg_list, int num_sg)
  111. {
  112. struct sg_table st;
  113. DBG("try unmapping %lu pages\n", (unsigned long)mem->page_count);
  114. pci_unmap_sg(intel_private.pcidev, sg_list,
  115. num_sg, PCI_DMA_BIDIRECTIONAL);
  116. st.sgl = sg_list;
  117. st.orig_nents = st.nents = num_sg;
  118. sg_free_table(&st);
  119. }
  120. static void intel_fake_agp_enable(struct agp_bridge_data *bridge, u32 mode)
  121. {
  122. return;
  123. }
  124. /* Exists to support ARGB cursors */
  125. static struct page *i8xx_alloc_pages(void)
  126. {
  127. struct page *page;
  128. page = alloc_pages(GFP_KERNEL | GFP_DMA32, 2);
  129. if (page == NULL)
  130. return NULL;
  131. if (set_pages_uc(page, 4) < 0) {
  132. set_pages_wb(page, 4);
  133. __free_pages(page, 2);
  134. return NULL;
  135. }
  136. atomic_inc(&agp_bridge->current_memory_agp);
  137. return page;
  138. }
  139. static void i8xx_destroy_pages(struct page *page)
  140. {
  141. if (page == NULL)
  142. return;
  143. set_pages_wb(page, 4);
  144. __free_pages(page, 2);
  145. atomic_dec(&agp_bridge->current_memory_agp);
  146. }
  147. #endif
  148. #define I810_GTT_ORDER 4
  149. static int i810_setup(void)
  150. {
  151. phys_addr_t reg_addr;
  152. char *gtt_table;
  153. /* i81x does not preallocate the gtt. It's always 64kb in size. */
  154. gtt_table = alloc_gatt_pages(I810_GTT_ORDER);
  155. if (gtt_table == NULL)
  156. return -ENOMEM;
  157. intel_private.i81x_gtt_table = gtt_table;
  158. reg_addr = pci_resource_start(intel_private.pcidev, I810_MMADR_BAR);
  159. intel_private.registers = ioremap(reg_addr, KB(64));
  160. if (!intel_private.registers)
  161. return -ENOMEM;
  162. writel(virt_to_phys(gtt_table) | I810_PGETBL_ENABLED,
  163. intel_private.registers+I810_PGETBL_CTL);
  164. intel_private.gtt_phys_addr = reg_addr + I810_PTE_BASE;
  165. if ((readl(intel_private.registers+I810_DRAM_CTL)
  166. & I810_DRAM_ROW_0) == I810_DRAM_ROW_0_SDRAM) {
  167. dev_info(&intel_private.pcidev->dev,
  168. "detected 4MB dedicated video ram\n");
  169. intel_private.num_dcache_entries = 1024;
  170. }
  171. return 0;
  172. }
  173. static void i810_cleanup(void)
  174. {
  175. writel(0, intel_private.registers+I810_PGETBL_CTL);
  176. free_gatt_pages(intel_private.i81x_gtt_table, I810_GTT_ORDER);
  177. }
  178. #if IS_ENABLED(CONFIG_AGP_INTEL)
  179. static int i810_insert_dcache_entries(struct agp_memory *mem, off_t pg_start,
  180. int type)
  181. {
  182. int i;
  183. if ((pg_start + mem->page_count)
  184. > intel_private.num_dcache_entries)
  185. return -EINVAL;
  186. if (!mem->is_flushed)
  187. global_cache_flush();
  188. for (i = pg_start; i < (pg_start + mem->page_count); i++) {
  189. dma_addr_t addr = i << PAGE_SHIFT;
  190. intel_private.driver->write_entry(addr,
  191. i, type);
  192. }
  193. wmb();
  194. return 0;
  195. }
  196. /*
  197. * The i810/i830 requires a physical address to program its mouse
  198. * pointer into hardware.
  199. * However the Xserver still writes to it through the agp aperture.
  200. */
  201. static struct agp_memory *alloc_agpphysmem_i8xx(size_t pg_count, int type)
  202. {
  203. struct agp_memory *new;
  204. struct page *page;
  205. switch (pg_count) {
  206. case 1: page = agp_bridge->driver->agp_alloc_page(agp_bridge);
  207. break;
  208. case 4:
  209. /* kludge to get 4 physical pages for ARGB cursor */
  210. page = i8xx_alloc_pages();
  211. break;
  212. default:
  213. return NULL;
  214. }
  215. if (page == NULL)
  216. return NULL;
  217. new = agp_create_memory(pg_count);
  218. if (new == NULL)
  219. return NULL;
  220. new->pages[0] = page;
  221. if (pg_count == 4) {
  222. /* kludge to get 4 physical pages for ARGB cursor */
  223. new->pages[1] = new->pages[0] + 1;
  224. new->pages[2] = new->pages[1] + 1;
  225. new->pages[3] = new->pages[2] + 1;
  226. }
  227. new->page_count = pg_count;
  228. new->num_scratch_pages = pg_count;
  229. new->type = AGP_PHYS_MEMORY;
  230. new->physical = page_to_phys(new->pages[0]);
  231. return new;
  232. }
  233. static void intel_i810_free_by_type(struct agp_memory *curr)
  234. {
  235. agp_free_key(curr->key);
  236. if (curr->type == AGP_PHYS_MEMORY) {
  237. if (curr->page_count == 4)
  238. i8xx_destroy_pages(curr->pages[0]);
  239. else {
  240. agp_bridge->driver->agp_destroy_page(curr->pages[0],
  241. AGP_PAGE_DESTROY_UNMAP);
  242. agp_bridge->driver->agp_destroy_page(curr->pages[0],
  243. AGP_PAGE_DESTROY_FREE);
  244. }
  245. agp_free_page_array(curr);
  246. }
  247. kfree(curr);
  248. }
  249. #endif
  250. static int intel_gtt_setup_scratch_page(void)
  251. {
  252. struct page *page;
  253. dma_addr_t dma_addr;
  254. page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
  255. if (page == NULL)
  256. return -ENOMEM;
  257. set_pages_uc(page, 1);
  258. if (intel_private.needs_dmar) {
  259. dma_addr = pci_map_page(intel_private.pcidev, page, 0,
  260. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  261. if (pci_dma_mapping_error(intel_private.pcidev, dma_addr))
  262. return -EINVAL;
  263. intel_private.scratch_page_dma = dma_addr;
  264. } else
  265. intel_private.scratch_page_dma = page_to_phys(page);
  266. intel_private.scratch_page = page;
  267. return 0;
  268. }
  269. static void i810_write_entry(dma_addr_t addr, unsigned int entry,
  270. unsigned int flags)
  271. {
  272. u32 pte_flags = I810_PTE_VALID;
  273. switch (flags) {
  274. case AGP_DCACHE_MEMORY:
  275. pte_flags |= I810_PTE_LOCAL;
  276. break;
  277. case AGP_USER_CACHED_MEMORY:
  278. pte_flags |= I830_PTE_SYSTEM_CACHED;
  279. break;
  280. }
  281. writel_relaxed(addr | pte_flags, intel_private.gtt + entry);
  282. }
  283. static const struct aper_size_info_fixed intel_fake_agp_sizes[] = {
  284. {32, 8192, 3},
  285. {64, 16384, 4},
  286. {128, 32768, 5},
  287. {256, 65536, 6},
  288. {512, 131072, 7},
  289. };
  290. static unsigned int intel_gtt_stolen_size(void)
  291. {
  292. u16 gmch_ctrl;
  293. u8 rdct;
  294. int local = 0;
  295. static const int ddt[4] = { 0, 16, 32, 64 };
  296. unsigned int stolen_size = 0;
  297. if (INTEL_GTT_GEN == 1)
  298. return 0; /* no stolen mem on i81x */
  299. pci_read_config_word(intel_private.bridge_dev,
  300. I830_GMCH_CTRL, &gmch_ctrl);
  301. if (intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82830_HB ||
  302. intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82845G_HB) {
  303. switch (gmch_ctrl & I830_GMCH_GMS_MASK) {
  304. case I830_GMCH_GMS_STOLEN_512:
  305. stolen_size = KB(512);
  306. break;
  307. case I830_GMCH_GMS_STOLEN_1024:
  308. stolen_size = MB(1);
  309. break;
  310. case I830_GMCH_GMS_STOLEN_8192:
  311. stolen_size = MB(8);
  312. break;
  313. case I830_GMCH_GMS_LOCAL:
  314. rdct = readb(intel_private.registers+I830_RDRAM_CHANNEL_TYPE);
  315. stolen_size = (I830_RDRAM_ND(rdct) + 1) *
  316. MB(ddt[I830_RDRAM_DDT(rdct)]);
  317. local = 1;
  318. break;
  319. default:
  320. stolen_size = 0;
  321. break;
  322. }
  323. } else {
  324. switch (gmch_ctrl & I855_GMCH_GMS_MASK) {
  325. case I855_GMCH_GMS_STOLEN_1M:
  326. stolen_size = MB(1);
  327. break;
  328. case I855_GMCH_GMS_STOLEN_4M:
  329. stolen_size = MB(4);
  330. break;
  331. case I855_GMCH_GMS_STOLEN_8M:
  332. stolen_size = MB(8);
  333. break;
  334. case I855_GMCH_GMS_STOLEN_16M:
  335. stolen_size = MB(16);
  336. break;
  337. case I855_GMCH_GMS_STOLEN_32M:
  338. stolen_size = MB(32);
  339. break;
  340. case I915_GMCH_GMS_STOLEN_48M:
  341. stolen_size = MB(48);
  342. break;
  343. case I915_GMCH_GMS_STOLEN_64M:
  344. stolen_size = MB(64);
  345. break;
  346. case G33_GMCH_GMS_STOLEN_128M:
  347. stolen_size = MB(128);
  348. break;
  349. case G33_GMCH_GMS_STOLEN_256M:
  350. stolen_size = MB(256);
  351. break;
  352. case INTEL_GMCH_GMS_STOLEN_96M:
  353. stolen_size = MB(96);
  354. break;
  355. case INTEL_GMCH_GMS_STOLEN_160M:
  356. stolen_size = MB(160);
  357. break;
  358. case INTEL_GMCH_GMS_STOLEN_224M:
  359. stolen_size = MB(224);
  360. break;
  361. case INTEL_GMCH_GMS_STOLEN_352M:
  362. stolen_size = MB(352);
  363. break;
  364. default:
  365. stolen_size = 0;
  366. break;
  367. }
  368. }
  369. if (stolen_size > 0) {
  370. dev_info(&intel_private.bridge_dev->dev, "detected %dK %s memory\n",
  371. stolen_size / KB(1), local ? "local" : "stolen");
  372. } else {
  373. dev_info(&intel_private.bridge_dev->dev,
  374. "no pre-allocated video memory detected\n");
  375. stolen_size = 0;
  376. }
  377. return stolen_size;
  378. }
  379. static void i965_adjust_pgetbl_size(unsigned int size_flag)
  380. {
  381. u32 pgetbl_ctl, pgetbl_ctl2;
  382. /* ensure that ppgtt is disabled */
  383. pgetbl_ctl2 = readl(intel_private.registers+I965_PGETBL_CTL2);
  384. pgetbl_ctl2 &= ~I810_PGETBL_ENABLED;
  385. writel(pgetbl_ctl2, intel_private.registers+I965_PGETBL_CTL2);
  386. /* write the new ggtt size */
  387. pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
  388. pgetbl_ctl &= ~I965_PGETBL_SIZE_MASK;
  389. pgetbl_ctl |= size_flag;
  390. writel(pgetbl_ctl, intel_private.registers+I810_PGETBL_CTL);
  391. }
  392. static unsigned int i965_gtt_total_entries(void)
  393. {
  394. int size;
  395. u32 pgetbl_ctl;
  396. u16 gmch_ctl;
  397. pci_read_config_word(intel_private.bridge_dev,
  398. I830_GMCH_CTRL, &gmch_ctl);
  399. if (INTEL_GTT_GEN == 5) {
  400. switch (gmch_ctl & G4x_GMCH_SIZE_MASK) {
  401. case G4x_GMCH_SIZE_1M:
  402. case G4x_GMCH_SIZE_VT_1M:
  403. i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1MB);
  404. break;
  405. case G4x_GMCH_SIZE_VT_1_5M:
  406. i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1_5MB);
  407. break;
  408. case G4x_GMCH_SIZE_2M:
  409. case G4x_GMCH_SIZE_VT_2M:
  410. i965_adjust_pgetbl_size(I965_PGETBL_SIZE_2MB);
  411. break;
  412. }
  413. }
  414. pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
  415. switch (pgetbl_ctl & I965_PGETBL_SIZE_MASK) {
  416. case I965_PGETBL_SIZE_128KB:
  417. size = KB(128);
  418. break;
  419. case I965_PGETBL_SIZE_256KB:
  420. size = KB(256);
  421. break;
  422. case I965_PGETBL_SIZE_512KB:
  423. size = KB(512);
  424. break;
  425. /* GTT pagetable sizes bigger than 512KB are not possible on G33! */
  426. case I965_PGETBL_SIZE_1MB:
  427. size = KB(1024);
  428. break;
  429. case I965_PGETBL_SIZE_2MB:
  430. size = KB(2048);
  431. break;
  432. case I965_PGETBL_SIZE_1_5MB:
  433. size = KB(1024 + 512);
  434. break;
  435. default:
  436. dev_info(&intel_private.pcidev->dev,
  437. "unknown page table size, assuming 512KB\n");
  438. size = KB(512);
  439. }
  440. return size/4;
  441. }
  442. static unsigned int intel_gtt_total_entries(void)
  443. {
  444. if (IS_G33 || INTEL_GTT_GEN == 4 || INTEL_GTT_GEN == 5)
  445. return i965_gtt_total_entries();
  446. else {
  447. /* On previous hardware, the GTT size was just what was
  448. * required to map the aperture.
  449. */
  450. return intel_private.gtt_mappable_entries;
  451. }
  452. }
  453. static unsigned int intel_gtt_mappable_entries(void)
  454. {
  455. unsigned int aperture_size;
  456. if (INTEL_GTT_GEN == 1) {
  457. u32 smram_miscc;
  458. pci_read_config_dword(intel_private.bridge_dev,
  459. I810_SMRAM_MISCC, &smram_miscc);
  460. if ((smram_miscc & I810_GFX_MEM_WIN_SIZE)
  461. == I810_GFX_MEM_WIN_32M)
  462. aperture_size = MB(32);
  463. else
  464. aperture_size = MB(64);
  465. } else if (INTEL_GTT_GEN == 2) {
  466. u16 gmch_ctrl;
  467. pci_read_config_word(intel_private.bridge_dev,
  468. I830_GMCH_CTRL, &gmch_ctrl);
  469. if ((gmch_ctrl & I830_GMCH_MEM_MASK) == I830_GMCH_MEM_64M)
  470. aperture_size = MB(64);
  471. else
  472. aperture_size = MB(128);
  473. } else {
  474. /* 9xx supports large sizes, just look at the length */
  475. aperture_size = pci_resource_len(intel_private.pcidev, 2);
  476. }
  477. return aperture_size >> PAGE_SHIFT;
  478. }
  479. static void intel_gtt_teardown_scratch_page(void)
  480. {
  481. set_pages_wb(intel_private.scratch_page, 1);
  482. if (intel_private.needs_dmar)
  483. pci_unmap_page(intel_private.pcidev,
  484. intel_private.scratch_page_dma,
  485. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  486. __free_page(intel_private.scratch_page);
  487. }
  488. static void intel_gtt_cleanup(void)
  489. {
  490. intel_private.driver->cleanup();
  491. iounmap(intel_private.gtt);
  492. iounmap(intel_private.registers);
  493. intel_gtt_teardown_scratch_page();
  494. }
  495. /* Certain Gen5 chipsets require require idling the GPU before
  496. * unmapping anything from the GTT when VT-d is enabled.
  497. */
  498. static inline int needs_ilk_vtd_wa(void)
  499. {
  500. #ifdef CONFIG_INTEL_IOMMU
  501. const unsigned short gpu_devid = intel_private.pcidev->device;
  502. /* Query intel_iommu to see if we need the workaround. Presumably that
  503. * was loaded first.
  504. */
  505. if ((gpu_devid == PCI_DEVICE_ID_INTEL_IRONLAKE_D_IG ||
  506. gpu_devid == PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG) &&
  507. intel_iommu_gfx_mapped)
  508. return 1;
  509. #endif
  510. return 0;
  511. }
  512. static bool intel_gtt_can_wc(void)
  513. {
  514. if (INTEL_GTT_GEN <= 2)
  515. return false;
  516. if (INTEL_GTT_GEN >= 6)
  517. return false;
  518. /* Reports of major corruption with ILK vt'd enabled */
  519. if (needs_ilk_vtd_wa())
  520. return false;
  521. return true;
  522. }
  523. static int intel_gtt_init(void)
  524. {
  525. u32 gtt_map_size;
  526. int ret, bar;
  527. ret = intel_private.driver->setup();
  528. if (ret != 0)
  529. return ret;
  530. intel_private.gtt_mappable_entries = intel_gtt_mappable_entries();
  531. intel_private.gtt_total_entries = intel_gtt_total_entries();
  532. /* save the PGETBL reg for resume */
  533. intel_private.PGETBL_save =
  534. readl(intel_private.registers+I810_PGETBL_CTL)
  535. & ~I810_PGETBL_ENABLED;
  536. /* we only ever restore the register when enabling the PGTBL... */
  537. if (HAS_PGTBL_EN)
  538. intel_private.PGETBL_save |= I810_PGETBL_ENABLED;
  539. dev_info(&intel_private.bridge_dev->dev,
  540. "detected gtt size: %dK total, %dK mappable\n",
  541. intel_private.gtt_total_entries * 4,
  542. intel_private.gtt_mappable_entries * 4);
  543. gtt_map_size = intel_private.gtt_total_entries * 4;
  544. intel_private.gtt = NULL;
  545. if (intel_gtt_can_wc())
  546. intel_private.gtt = ioremap_wc(intel_private.gtt_phys_addr,
  547. gtt_map_size);
  548. if (intel_private.gtt == NULL)
  549. intel_private.gtt = ioremap(intel_private.gtt_phys_addr,
  550. gtt_map_size);
  551. if (intel_private.gtt == NULL) {
  552. intel_private.driver->cleanup();
  553. iounmap(intel_private.registers);
  554. return -ENOMEM;
  555. }
  556. #if IS_ENABLED(CONFIG_AGP_INTEL)
  557. global_cache_flush(); /* FIXME: ? */
  558. #endif
  559. intel_private.stolen_size = intel_gtt_stolen_size();
  560. intel_private.needs_dmar = USE_PCI_DMA_API && INTEL_GTT_GEN > 2;
  561. ret = intel_gtt_setup_scratch_page();
  562. if (ret != 0) {
  563. intel_gtt_cleanup();
  564. return ret;
  565. }
  566. if (INTEL_GTT_GEN <= 2)
  567. bar = I810_GMADR_BAR;
  568. else
  569. bar = I915_GMADR_BAR;
  570. intel_private.gma_bus_addr = pci_bus_address(intel_private.pcidev, bar);
  571. return 0;
  572. }
  573. #if IS_ENABLED(CONFIG_AGP_INTEL)
  574. static int intel_fake_agp_fetch_size(void)
  575. {
  576. int num_sizes = ARRAY_SIZE(intel_fake_agp_sizes);
  577. unsigned int aper_size;
  578. int i;
  579. aper_size = (intel_private.gtt_mappable_entries << PAGE_SHIFT) / MB(1);
  580. for (i = 0; i < num_sizes; i++) {
  581. if (aper_size == intel_fake_agp_sizes[i].size) {
  582. agp_bridge->current_size =
  583. (void *) (intel_fake_agp_sizes + i);
  584. return aper_size;
  585. }
  586. }
  587. return 0;
  588. }
  589. #endif
  590. static void i830_cleanup(void)
  591. {
  592. }
  593. /* The chipset_flush interface needs to get data that has already been
  594. * flushed out of the CPU all the way out to main memory, because the GPU
  595. * doesn't snoop those buffers.
  596. *
  597. * The 8xx series doesn't have the same lovely interface for flushing the
  598. * chipset write buffers that the later chips do. According to the 865
  599. * specs, it's 64 octwords, or 1KB. So, to get those previous things in
  600. * that buffer out, we just fill 1KB and clflush it out, on the assumption
  601. * that it'll push whatever was in there out. It appears to work.
  602. */
  603. static void i830_chipset_flush(void)
  604. {
  605. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  606. /* Forcibly evict everything from the CPU write buffers.
  607. * clflush appears to be insufficient.
  608. */
  609. wbinvd_on_all_cpus();
  610. /* Now we've only seen documents for this magic bit on 855GM,
  611. * we hope it exists for the other gen2 chipsets...
  612. *
  613. * Also works as advertised on my 845G.
  614. */
  615. writel(readl(intel_private.registers+I830_HIC) | (1<<31),
  616. intel_private.registers+I830_HIC);
  617. while (readl(intel_private.registers+I830_HIC) & (1<<31)) {
  618. if (time_after(jiffies, timeout))
  619. break;
  620. udelay(50);
  621. }
  622. }
  623. static void i830_write_entry(dma_addr_t addr, unsigned int entry,
  624. unsigned int flags)
  625. {
  626. u32 pte_flags = I810_PTE_VALID;
  627. if (flags == AGP_USER_CACHED_MEMORY)
  628. pte_flags |= I830_PTE_SYSTEM_CACHED;
  629. writel_relaxed(addr | pte_flags, intel_private.gtt + entry);
  630. }
  631. bool intel_enable_gtt(void)
  632. {
  633. u8 __iomem *reg;
  634. if (INTEL_GTT_GEN == 2) {
  635. u16 gmch_ctrl;
  636. pci_read_config_word(intel_private.bridge_dev,
  637. I830_GMCH_CTRL, &gmch_ctrl);
  638. gmch_ctrl |= I830_GMCH_ENABLED;
  639. pci_write_config_word(intel_private.bridge_dev,
  640. I830_GMCH_CTRL, gmch_ctrl);
  641. pci_read_config_word(intel_private.bridge_dev,
  642. I830_GMCH_CTRL, &gmch_ctrl);
  643. if ((gmch_ctrl & I830_GMCH_ENABLED) == 0) {
  644. dev_err(&intel_private.pcidev->dev,
  645. "failed to enable the GTT: GMCH_CTRL=%x\n",
  646. gmch_ctrl);
  647. return false;
  648. }
  649. }
  650. /* On the resume path we may be adjusting the PGTBL value, so
  651. * be paranoid and flush all chipset write buffers...
  652. */
  653. if (INTEL_GTT_GEN >= 3)
  654. writel(0, intel_private.registers+GFX_FLSH_CNTL);
  655. reg = intel_private.registers+I810_PGETBL_CTL;
  656. writel(intel_private.PGETBL_save, reg);
  657. if (HAS_PGTBL_EN && (readl(reg) & I810_PGETBL_ENABLED) == 0) {
  658. dev_err(&intel_private.pcidev->dev,
  659. "failed to enable the GTT: PGETBL=%x [expected %x]\n",
  660. readl(reg), intel_private.PGETBL_save);
  661. return false;
  662. }
  663. if (INTEL_GTT_GEN >= 3)
  664. writel(0, intel_private.registers+GFX_FLSH_CNTL);
  665. return true;
  666. }
  667. EXPORT_SYMBOL(intel_enable_gtt);
  668. static int i830_setup(void)
  669. {
  670. phys_addr_t reg_addr;
  671. reg_addr = pci_resource_start(intel_private.pcidev, I810_MMADR_BAR);
  672. intel_private.registers = ioremap(reg_addr, KB(64));
  673. if (!intel_private.registers)
  674. return -ENOMEM;
  675. intel_private.gtt_phys_addr = reg_addr + I810_PTE_BASE;
  676. return 0;
  677. }
  678. #if IS_ENABLED(CONFIG_AGP_INTEL)
  679. static int intel_fake_agp_create_gatt_table(struct agp_bridge_data *bridge)
  680. {
  681. agp_bridge->gatt_table_real = NULL;
  682. agp_bridge->gatt_table = NULL;
  683. agp_bridge->gatt_bus_addr = 0;
  684. return 0;
  685. }
  686. static int intel_fake_agp_free_gatt_table(struct agp_bridge_data *bridge)
  687. {
  688. return 0;
  689. }
  690. static int intel_fake_agp_configure(void)
  691. {
  692. if (!intel_enable_gtt())
  693. return -EIO;
  694. intel_private.clear_fake_agp = true;
  695. agp_bridge->gart_bus_addr = intel_private.gma_bus_addr;
  696. return 0;
  697. }
  698. #endif
  699. static bool i830_check_flags(unsigned int flags)
  700. {
  701. switch (flags) {
  702. case 0:
  703. case AGP_PHYS_MEMORY:
  704. case AGP_USER_CACHED_MEMORY:
  705. case AGP_USER_MEMORY:
  706. return true;
  707. }
  708. return false;
  709. }
  710. void intel_gtt_insert_page(dma_addr_t addr,
  711. unsigned int pg,
  712. unsigned int flags)
  713. {
  714. intel_private.driver->write_entry(addr, pg, flags);
  715. if (intel_private.driver->chipset_flush)
  716. intel_private.driver->chipset_flush();
  717. }
  718. EXPORT_SYMBOL(intel_gtt_insert_page);
  719. void intel_gtt_insert_sg_entries(struct sg_table *st,
  720. unsigned int pg_start,
  721. unsigned int flags)
  722. {
  723. struct scatterlist *sg;
  724. unsigned int len, m;
  725. int i, j;
  726. j = pg_start;
  727. /* sg may merge pages, but we have to separate
  728. * per-page addr for GTT */
  729. for_each_sg(st->sgl, sg, st->nents, i) {
  730. len = sg_dma_len(sg) >> PAGE_SHIFT;
  731. for (m = 0; m < len; m++) {
  732. dma_addr_t addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
  733. intel_private.driver->write_entry(addr, j, flags);
  734. j++;
  735. }
  736. }
  737. wmb();
  738. if (intel_private.driver->chipset_flush)
  739. intel_private.driver->chipset_flush();
  740. }
  741. EXPORT_SYMBOL(intel_gtt_insert_sg_entries);
  742. #if IS_ENABLED(CONFIG_AGP_INTEL)
  743. static void intel_gtt_insert_pages(unsigned int first_entry,
  744. unsigned int num_entries,
  745. struct page **pages,
  746. unsigned int flags)
  747. {
  748. int i, j;
  749. for (i = 0, j = first_entry; i < num_entries; i++, j++) {
  750. dma_addr_t addr = page_to_phys(pages[i]);
  751. intel_private.driver->write_entry(addr,
  752. j, flags);
  753. }
  754. wmb();
  755. }
  756. static int intel_fake_agp_insert_entries(struct agp_memory *mem,
  757. off_t pg_start, int type)
  758. {
  759. int ret = -EINVAL;
  760. if (intel_private.clear_fake_agp) {
  761. int start = intel_private.stolen_size / PAGE_SIZE;
  762. int end = intel_private.gtt_mappable_entries;
  763. intel_gtt_clear_range(start, end - start);
  764. intel_private.clear_fake_agp = false;
  765. }
  766. if (INTEL_GTT_GEN == 1 && type == AGP_DCACHE_MEMORY)
  767. return i810_insert_dcache_entries(mem, pg_start, type);
  768. if (mem->page_count == 0)
  769. goto out;
  770. if (pg_start + mem->page_count > intel_private.gtt_total_entries)
  771. goto out_err;
  772. if (type != mem->type)
  773. goto out_err;
  774. if (!intel_private.driver->check_flags(type))
  775. goto out_err;
  776. if (!mem->is_flushed)
  777. global_cache_flush();
  778. if (intel_private.needs_dmar) {
  779. struct sg_table st;
  780. ret = intel_gtt_map_memory(mem->pages, mem->page_count, &st);
  781. if (ret != 0)
  782. return ret;
  783. intel_gtt_insert_sg_entries(&st, pg_start, type);
  784. mem->sg_list = st.sgl;
  785. mem->num_sg = st.nents;
  786. } else
  787. intel_gtt_insert_pages(pg_start, mem->page_count, mem->pages,
  788. type);
  789. out:
  790. ret = 0;
  791. out_err:
  792. mem->is_flushed = true;
  793. return ret;
  794. }
  795. #endif
  796. void intel_gtt_clear_range(unsigned int first_entry, unsigned int num_entries)
  797. {
  798. unsigned int i;
  799. for (i = first_entry; i < (first_entry + num_entries); i++) {
  800. intel_private.driver->write_entry(intel_private.scratch_page_dma,
  801. i, 0);
  802. }
  803. wmb();
  804. }
  805. EXPORT_SYMBOL(intel_gtt_clear_range);
  806. #if IS_ENABLED(CONFIG_AGP_INTEL)
  807. static int intel_fake_agp_remove_entries(struct agp_memory *mem,
  808. off_t pg_start, int type)
  809. {
  810. if (mem->page_count == 0)
  811. return 0;
  812. intel_gtt_clear_range(pg_start, mem->page_count);
  813. if (intel_private.needs_dmar) {
  814. intel_gtt_unmap_memory(mem->sg_list, mem->num_sg);
  815. mem->sg_list = NULL;
  816. mem->num_sg = 0;
  817. }
  818. return 0;
  819. }
  820. static struct agp_memory *intel_fake_agp_alloc_by_type(size_t pg_count,
  821. int type)
  822. {
  823. struct agp_memory *new;
  824. if (type == AGP_DCACHE_MEMORY && INTEL_GTT_GEN == 1) {
  825. if (pg_count != intel_private.num_dcache_entries)
  826. return NULL;
  827. new = agp_create_memory(1);
  828. if (new == NULL)
  829. return NULL;
  830. new->type = AGP_DCACHE_MEMORY;
  831. new->page_count = pg_count;
  832. new->num_scratch_pages = 0;
  833. agp_free_page_array(new);
  834. return new;
  835. }
  836. if (type == AGP_PHYS_MEMORY)
  837. return alloc_agpphysmem_i8xx(pg_count, type);
  838. /* always return NULL for other allocation types for now */
  839. return NULL;
  840. }
  841. #endif
  842. static int intel_alloc_chipset_flush_resource(void)
  843. {
  844. int ret;
  845. ret = pci_bus_alloc_resource(intel_private.bridge_dev->bus, &intel_private.ifp_resource, PAGE_SIZE,
  846. PAGE_SIZE, PCIBIOS_MIN_MEM, 0,
  847. pcibios_align_resource, intel_private.bridge_dev);
  848. return ret;
  849. }
  850. static void intel_i915_setup_chipset_flush(void)
  851. {
  852. int ret;
  853. u32 temp;
  854. pci_read_config_dword(intel_private.bridge_dev, I915_IFPADDR, &temp);
  855. if (!(temp & 0x1)) {
  856. intel_alloc_chipset_flush_resource();
  857. intel_private.resource_valid = 1;
  858. pci_write_config_dword(intel_private.bridge_dev, I915_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
  859. } else {
  860. temp &= ~1;
  861. intel_private.resource_valid = 1;
  862. intel_private.ifp_resource.start = temp;
  863. intel_private.ifp_resource.end = temp + PAGE_SIZE;
  864. ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
  865. /* some BIOSes reserve this area in a pnp some don't */
  866. if (ret)
  867. intel_private.resource_valid = 0;
  868. }
  869. }
  870. static void intel_i965_g33_setup_chipset_flush(void)
  871. {
  872. u32 temp_hi, temp_lo;
  873. int ret;
  874. pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4, &temp_hi);
  875. pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR, &temp_lo);
  876. if (!(temp_lo & 0x1)) {
  877. intel_alloc_chipset_flush_resource();
  878. intel_private.resource_valid = 1;
  879. pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4,
  880. upper_32_bits(intel_private.ifp_resource.start));
  881. pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
  882. } else {
  883. u64 l64;
  884. temp_lo &= ~0x1;
  885. l64 = ((u64)temp_hi << 32) | temp_lo;
  886. intel_private.resource_valid = 1;
  887. intel_private.ifp_resource.start = l64;
  888. intel_private.ifp_resource.end = l64 + PAGE_SIZE;
  889. ret = request_resource(&iomem_resource, &intel_private.ifp_resource);
  890. /* some BIOSes reserve this area in a pnp some don't */
  891. if (ret)
  892. intel_private.resource_valid = 0;
  893. }
  894. }
  895. static void intel_i9xx_setup_flush(void)
  896. {
  897. /* return if already configured */
  898. if (intel_private.ifp_resource.start)
  899. return;
  900. if (INTEL_GTT_GEN == 6)
  901. return;
  902. /* setup a resource for this object */
  903. intel_private.ifp_resource.name = "Intel Flush Page";
  904. intel_private.ifp_resource.flags = IORESOURCE_MEM;
  905. /* Setup chipset flush for 915 */
  906. if (IS_G33 || INTEL_GTT_GEN >= 4) {
  907. intel_i965_g33_setup_chipset_flush();
  908. } else {
  909. intel_i915_setup_chipset_flush();
  910. }
  911. if (intel_private.ifp_resource.start)
  912. intel_private.i9xx_flush_page = ioremap_nocache(intel_private.ifp_resource.start, PAGE_SIZE);
  913. if (!intel_private.i9xx_flush_page)
  914. dev_err(&intel_private.pcidev->dev,
  915. "can't ioremap flush page - no chipset flushing\n");
  916. }
  917. static void i9xx_cleanup(void)
  918. {
  919. if (intel_private.i9xx_flush_page)
  920. iounmap(intel_private.i9xx_flush_page);
  921. if (intel_private.resource_valid)
  922. release_resource(&intel_private.ifp_resource);
  923. intel_private.ifp_resource.start = 0;
  924. intel_private.resource_valid = 0;
  925. }
  926. static void i9xx_chipset_flush(void)
  927. {
  928. if (intel_private.i9xx_flush_page)
  929. writel(1, intel_private.i9xx_flush_page);
  930. }
  931. static void i965_write_entry(dma_addr_t addr,
  932. unsigned int entry,
  933. unsigned int flags)
  934. {
  935. u32 pte_flags;
  936. pte_flags = I810_PTE_VALID;
  937. if (flags == AGP_USER_CACHED_MEMORY)
  938. pte_flags |= I830_PTE_SYSTEM_CACHED;
  939. /* Shift high bits down */
  940. addr |= (addr >> 28) & 0xf0;
  941. writel_relaxed(addr | pte_flags, intel_private.gtt + entry);
  942. }
  943. static int i9xx_setup(void)
  944. {
  945. phys_addr_t reg_addr;
  946. int size = KB(512);
  947. reg_addr = pci_resource_start(intel_private.pcidev, I915_MMADR_BAR);
  948. intel_private.registers = ioremap(reg_addr, size);
  949. if (!intel_private.registers)
  950. return -ENOMEM;
  951. switch (INTEL_GTT_GEN) {
  952. case 3:
  953. intel_private.gtt_phys_addr =
  954. pci_resource_start(intel_private.pcidev, I915_PTE_BAR);
  955. break;
  956. case 5:
  957. intel_private.gtt_phys_addr = reg_addr + MB(2);
  958. break;
  959. default:
  960. intel_private.gtt_phys_addr = reg_addr + KB(512);
  961. break;
  962. }
  963. intel_i9xx_setup_flush();
  964. return 0;
  965. }
  966. #if IS_ENABLED(CONFIG_AGP_INTEL)
  967. static const struct agp_bridge_driver intel_fake_agp_driver = {
  968. .owner = THIS_MODULE,
  969. .size_type = FIXED_APER_SIZE,
  970. .aperture_sizes = intel_fake_agp_sizes,
  971. .num_aperture_sizes = ARRAY_SIZE(intel_fake_agp_sizes),
  972. .configure = intel_fake_agp_configure,
  973. .fetch_size = intel_fake_agp_fetch_size,
  974. .cleanup = intel_gtt_cleanup,
  975. .agp_enable = intel_fake_agp_enable,
  976. .cache_flush = global_cache_flush,
  977. .create_gatt_table = intel_fake_agp_create_gatt_table,
  978. .free_gatt_table = intel_fake_agp_free_gatt_table,
  979. .insert_memory = intel_fake_agp_insert_entries,
  980. .remove_memory = intel_fake_agp_remove_entries,
  981. .alloc_by_type = intel_fake_agp_alloc_by_type,
  982. .free_by_type = intel_i810_free_by_type,
  983. .agp_alloc_page = agp_generic_alloc_page,
  984. .agp_alloc_pages = agp_generic_alloc_pages,
  985. .agp_destroy_page = agp_generic_destroy_page,
  986. .agp_destroy_pages = agp_generic_destroy_pages,
  987. };
  988. #endif
  989. static const struct intel_gtt_driver i81x_gtt_driver = {
  990. .gen = 1,
  991. .has_pgtbl_enable = 1,
  992. .dma_mask_size = 32,
  993. .setup = i810_setup,
  994. .cleanup = i810_cleanup,
  995. .check_flags = i830_check_flags,
  996. .write_entry = i810_write_entry,
  997. };
  998. static const struct intel_gtt_driver i8xx_gtt_driver = {
  999. .gen = 2,
  1000. .has_pgtbl_enable = 1,
  1001. .setup = i830_setup,
  1002. .cleanup = i830_cleanup,
  1003. .write_entry = i830_write_entry,
  1004. .dma_mask_size = 32,
  1005. .check_flags = i830_check_flags,
  1006. .chipset_flush = i830_chipset_flush,
  1007. };
  1008. static const struct intel_gtt_driver i915_gtt_driver = {
  1009. .gen = 3,
  1010. .has_pgtbl_enable = 1,
  1011. .setup = i9xx_setup,
  1012. .cleanup = i9xx_cleanup,
  1013. /* i945 is the last gpu to need phys mem (for overlay and cursors). */
  1014. .write_entry = i830_write_entry,
  1015. .dma_mask_size = 32,
  1016. .check_flags = i830_check_flags,
  1017. .chipset_flush = i9xx_chipset_flush,
  1018. };
  1019. static const struct intel_gtt_driver g33_gtt_driver = {
  1020. .gen = 3,
  1021. .is_g33 = 1,
  1022. .setup = i9xx_setup,
  1023. .cleanup = i9xx_cleanup,
  1024. .write_entry = i965_write_entry,
  1025. .dma_mask_size = 36,
  1026. .check_flags = i830_check_flags,
  1027. .chipset_flush = i9xx_chipset_flush,
  1028. };
  1029. static const struct intel_gtt_driver pineview_gtt_driver = {
  1030. .gen = 3,
  1031. .is_pineview = 1, .is_g33 = 1,
  1032. .setup = i9xx_setup,
  1033. .cleanup = i9xx_cleanup,
  1034. .write_entry = i965_write_entry,
  1035. .dma_mask_size = 36,
  1036. .check_flags = i830_check_flags,
  1037. .chipset_flush = i9xx_chipset_flush,
  1038. };
  1039. static const struct intel_gtt_driver i965_gtt_driver = {
  1040. .gen = 4,
  1041. .has_pgtbl_enable = 1,
  1042. .setup = i9xx_setup,
  1043. .cleanup = i9xx_cleanup,
  1044. .write_entry = i965_write_entry,
  1045. .dma_mask_size = 36,
  1046. .check_flags = i830_check_flags,
  1047. .chipset_flush = i9xx_chipset_flush,
  1048. };
  1049. static const struct intel_gtt_driver g4x_gtt_driver = {
  1050. .gen = 5,
  1051. .setup = i9xx_setup,
  1052. .cleanup = i9xx_cleanup,
  1053. .write_entry = i965_write_entry,
  1054. .dma_mask_size = 36,
  1055. .check_flags = i830_check_flags,
  1056. .chipset_flush = i9xx_chipset_flush,
  1057. };
  1058. static const struct intel_gtt_driver ironlake_gtt_driver = {
  1059. .gen = 5,
  1060. .is_ironlake = 1,
  1061. .setup = i9xx_setup,
  1062. .cleanup = i9xx_cleanup,
  1063. .write_entry = i965_write_entry,
  1064. .dma_mask_size = 36,
  1065. .check_flags = i830_check_flags,
  1066. .chipset_flush = i9xx_chipset_flush,
  1067. };
  1068. /* Table to describe Intel GMCH and AGP/PCIE GART drivers. At least one of
  1069. * driver and gmch_driver must be non-null, and find_gmch will determine
  1070. * which one should be used if a gmch_chip_id is present.
  1071. */
  1072. static const struct intel_gtt_driver_description {
  1073. unsigned int gmch_chip_id;
  1074. char *name;
  1075. const struct intel_gtt_driver *gtt_driver;
  1076. } intel_gtt_chipsets[] = {
  1077. { PCI_DEVICE_ID_INTEL_82810_IG1, "i810",
  1078. &i81x_gtt_driver},
  1079. { PCI_DEVICE_ID_INTEL_82810_IG3, "i810",
  1080. &i81x_gtt_driver},
  1081. { PCI_DEVICE_ID_INTEL_82810E_IG, "i810",
  1082. &i81x_gtt_driver},
  1083. { PCI_DEVICE_ID_INTEL_82815_CGC, "i815",
  1084. &i81x_gtt_driver},
  1085. { PCI_DEVICE_ID_INTEL_82830_CGC, "830M",
  1086. &i8xx_gtt_driver},
  1087. { PCI_DEVICE_ID_INTEL_82845G_IG, "845G",
  1088. &i8xx_gtt_driver},
  1089. { PCI_DEVICE_ID_INTEL_82854_IG, "854",
  1090. &i8xx_gtt_driver},
  1091. { PCI_DEVICE_ID_INTEL_82855GM_IG, "855GM",
  1092. &i8xx_gtt_driver},
  1093. { PCI_DEVICE_ID_INTEL_82865_IG, "865",
  1094. &i8xx_gtt_driver},
  1095. { PCI_DEVICE_ID_INTEL_E7221_IG, "E7221 (i915)",
  1096. &i915_gtt_driver },
  1097. { PCI_DEVICE_ID_INTEL_82915G_IG, "915G",
  1098. &i915_gtt_driver },
  1099. { PCI_DEVICE_ID_INTEL_82915GM_IG, "915GM",
  1100. &i915_gtt_driver },
  1101. { PCI_DEVICE_ID_INTEL_82945G_IG, "945G",
  1102. &i915_gtt_driver },
  1103. { PCI_DEVICE_ID_INTEL_82945GM_IG, "945GM",
  1104. &i915_gtt_driver },
  1105. { PCI_DEVICE_ID_INTEL_82945GME_IG, "945GME",
  1106. &i915_gtt_driver },
  1107. { PCI_DEVICE_ID_INTEL_82946GZ_IG, "946GZ",
  1108. &i965_gtt_driver },
  1109. { PCI_DEVICE_ID_INTEL_82G35_IG, "G35",
  1110. &i965_gtt_driver },
  1111. { PCI_DEVICE_ID_INTEL_82965Q_IG, "965Q",
  1112. &i965_gtt_driver },
  1113. { PCI_DEVICE_ID_INTEL_82965G_IG, "965G",
  1114. &i965_gtt_driver },
  1115. { PCI_DEVICE_ID_INTEL_82965GM_IG, "965GM",
  1116. &i965_gtt_driver },
  1117. { PCI_DEVICE_ID_INTEL_82965GME_IG, "965GME/GLE",
  1118. &i965_gtt_driver },
  1119. { PCI_DEVICE_ID_INTEL_G33_IG, "G33",
  1120. &g33_gtt_driver },
  1121. { PCI_DEVICE_ID_INTEL_Q35_IG, "Q35",
  1122. &g33_gtt_driver },
  1123. { PCI_DEVICE_ID_INTEL_Q33_IG, "Q33",
  1124. &g33_gtt_driver },
  1125. { PCI_DEVICE_ID_INTEL_PINEVIEW_M_IG, "GMA3150",
  1126. &pineview_gtt_driver },
  1127. { PCI_DEVICE_ID_INTEL_PINEVIEW_IG, "GMA3150",
  1128. &pineview_gtt_driver },
  1129. { PCI_DEVICE_ID_INTEL_GM45_IG, "GM45",
  1130. &g4x_gtt_driver },
  1131. { PCI_DEVICE_ID_INTEL_EAGLELAKE_IG, "Eaglelake",
  1132. &g4x_gtt_driver },
  1133. { PCI_DEVICE_ID_INTEL_Q45_IG, "Q45/Q43",
  1134. &g4x_gtt_driver },
  1135. { PCI_DEVICE_ID_INTEL_G45_IG, "G45/G43",
  1136. &g4x_gtt_driver },
  1137. { PCI_DEVICE_ID_INTEL_B43_IG, "B43",
  1138. &g4x_gtt_driver },
  1139. { PCI_DEVICE_ID_INTEL_B43_1_IG, "B43",
  1140. &g4x_gtt_driver },
  1141. { PCI_DEVICE_ID_INTEL_G41_IG, "G41",
  1142. &g4x_gtt_driver },
  1143. { PCI_DEVICE_ID_INTEL_IRONLAKE_D_IG,
  1144. "HD Graphics", &ironlake_gtt_driver },
  1145. { PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG,
  1146. "HD Graphics", &ironlake_gtt_driver },
  1147. { 0, NULL, NULL }
  1148. };
  1149. static int find_gmch(u16 device)
  1150. {
  1151. struct pci_dev *gmch_device;
  1152. gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL, device, NULL);
  1153. if (gmch_device && PCI_FUNC(gmch_device->devfn) != 0) {
  1154. gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL,
  1155. device, gmch_device);
  1156. }
  1157. if (!gmch_device)
  1158. return 0;
  1159. intel_private.pcidev = gmch_device;
  1160. return 1;
  1161. }
  1162. int intel_gmch_probe(struct pci_dev *bridge_pdev, struct pci_dev *gpu_pdev,
  1163. struct agp_bridge_data *bridge)
  1164. {
  1165. int i, mask;
  1166. for (i = 0; intel_gtt_chipsets[i].name != NULL; i++) {
  1167. if (gpu_pdev) {
  1168. if (gpu_pdev->device ==
  1169. intel_gtt_chipsets[i].gmch_chip_id) {
  1170. intel_private.pcidev = pci_dev_get(gpu_pdev);
  1171. intel_private.driver =
  1172. intel_gtt_chipsets[i].gtt_driver;
  1173. break;
  1174. }
  1175. } else if (find_gmch(intel_gtt_chipsets[i].gmch_chip_id)) {
  1176. intel_private.driver =
  1177. intel_gtt_chipsets[i].gtt_driver;
  1178. break;
  1179. }
  1180. }
  1181. if (!intel_private.driver)
  1182. return 0;
  1183. #if IS_ENABLED(CONFIG_AGP_INTEL)
  1184. if (bridge) {
  1185. if (INTEL_GTT_GEN > 1)
  1186. return 0;
  1187. bridge->driver = &intel_fake_agp_driver;
  1188. bridge->dev_private_data = &intel_private;
  1189. bridge->dev = bridge_pdev;
  1190. }
  1191. #endif
  1192. /*
  1193. * Can be called from the fake agp driver but also directly from
  1194. * drm/i915.ko. Hence we need to check whether everything is set up
  1195. * already.
  1196. */
  1197. if (intel_private.refcount++)
  1198. return 1;
  1199. intel_private.bridge_dev = pci_dev_get(bridge_pdev);
  1200. dev_info(&bridge_pdev->dev, "Intel %s Chipset\n", intel_gtt_chipsets[i].name);
  1201. mask = intel_private.driver->dma_mask_size;
  1202. if (pci_set_dma_mask(intel_private.pcidev, DMA_BIT_MASK(mask)))
  1203. dev_err(&intel_private.pcidev->dev,
  1204. "set gfx device dma mask %d-bit failed!\n", mask);
  1205. else
  1206. pci_set_consistent_dma_mask(intel_private.pcidev,
  1207. DMA_BIT_MASK(mask));
  1208. if (intel_gtt_init() != 0) {
  1209. intel_gmch_remove();
  1210. return 0;
  1211. }
  1212. return 1;
  1213. }
  1214. EXPORT_SYMBOL(intel_gmch_probe);
  1215. void intel_gtt_get(u64 *gtt_total, size_t *stolen_size,
  1216. phys_addr_t *mappable_base, u64 *mappable_end)
  1217. {
  1218. *gtt_total = intel_private.gtt_total_entries << PAGE_SHIFT;
  1219. *stolen_size = intel_private.stolen_size;
  1220. *mappable_base = intel_private.gma_bus_addr;
  1221. *mappable_end = intel_private.gtt_mappable_entries << PAGE_SHIFT;
  1222. }
  1223. EXPORT_SYMBOL(intel_gtt_get);
  1224. void intel_gtt_chipset_flush(void)
  1225. {
  1226. if (intel_private.driver->chipset_flush)
  1227. intel_private.driver->chipset_flush();
  1228. }
  1229. EXPORT_SYMBOL(intel_gtt_chipset_flush);
  1230. void intel_gmch_remove(void)
  1231. {
  1232. if (--intel_private.refcount)
  1233. return;
  1234. if (intel_private.scratch_page)
  1235. intel_gtt_teardown_scratch_page();
  1236. if (intel_private.pcidev)
  1237. pci_dev_put(intel_private.pcidev);
  1238. if (intel_private.bridge_dev)
  1239. pci_dev_put(intel_private.bridge_dev);
  1240. intel_private.driver = NULL;
  1241. }
  1242. EXPORT_SYMBOL(intel_gmch_remove);
  1243. MODULE_AUTHOR("Dave Jones, Various @Intel");
  1244. MODULE_LICENSE("GPL and additional rights");