intel-gtt.c 36 KB

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