intel-gtt.c 39 KB

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