intel-gtt.c 40 KB

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