intel-gtt.c 35 KB

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