intel-gtt.c 41 KB

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