intel-gtt.c 36 KB

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