intel-gtt.c 37 KB

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