intel-gtt.c 35 KB

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