tioce_provider.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2003-2006 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. #include <linux/types.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/pci.h>
  11. #include <asm/sn/sn_sal.h>
  12. #include <asm/sn/addrs.h>
  13. #include <asm/sn/io.h>
  14. #include <asm/sn/pcidev.h>
  15. #include <asm/sn/pcibus_provider_defs.h>
  16. #include <asm/sn/tioce_provider.h>
  17. /*
  18. * 1/26/2006
  19. *
  20. * WAR for SGI PV 944642. For revA TIOCE, need to use the following recipe
  21. * (taken from the above PV) before and after accessing tioce internal MMR's
  22. * to avoid tioce lockups.
  23. *
  24. * The recipe as taken from the PV:
  25. *
  26. * if(mmr address < 0x45000) {
  27. * if(mmr address == 0 or 0x80)
  28. * mmr wrt or read address 0xc0
  29. * else if(mmr address == 0x148 or 0x200)
  30. * mmr wrt or read address 0x28
  31. * else
  32. * mmr wrt or read address 0x158
  33. *
  34. * do desired mmr access (rd or wrt)
  35. *
  36. * if(mmr address == 0x100)
  37. * mmr wrt or read address 0x38
  38. * mmr wrt or read address 0xb050
  39. * } else
  40. * do desired mmr access
  41. *
  42. * According to hw, we can use reads instead of writes to the above addres
  43. *
  44. * Note this WAR can only to be used for accessing internal MMR's in the
  45. * TIOCE Coretalk Address Range 0x0 - 0x07ff_ffff. This includes the
  46. * "Local CE Registers and Memories" and "PCI Compatible Config Space" address
  47. * spaces from table 2-1 of the "CE Programmer's Reference Overview" document.
  48. *
  49. * All registers defined in struct tioce will meet that criteria.
  50. */
  51. static void inline
  52. tioce_mmr_war_pre(struct tioce_kernel *kern, void __iomem *mmr_addr)
  53. {
  54. u64 mmr_base;
  55. u64 mmr_offset;
  56. if (kern->ce_common->ce_rev != TIOCE_REV_A)
  57. return;
  58. mmr_base = kern->ce_common->ce_pcibus.bs_base;
  59. mmr_offset = (unsigned long)mmr_addr - mmr_base;
  60. if (mmr_offset < 0x45000) {
  61. u64 mmr_war_offset;
  62. if (mmr_offset == 0 || mmr_offset == 0x80)
  63. mmr_war_offset = 0xc0;
  64. else if (mmr_offset == 0x148 || mmr_offset == 0x200)
  65. mmr_war_offset = 0x28;
  66. else
  67. mmr_war_offset = 0x158;
  68. readq_relaxed((void __iomem *)(mmr_base + mmr_war_offset));
  69. }
  70. }
  71. static void inline
  72. tioce_mmr_war_post(struct tioce_kernel *kern, void __iomem *mmr_addr)
  73. {
  74. u64 mmr_base;
  75. u64 mmr_offset;
  76. if (kern->ce_common->ce_rev != TIOCE_REV_A)
  77. return;
  78. mmr_base = kern->ce_common->ce_pcibus.bs_base;
  79. mmr_offset = (unsigned long)mmr_addr - mmr_base;
  80. if (mmr_offset < 0x45000) {
  81. if (mmr_offset == 0x100)
  82. readq_relaxed((void __iomem *)(mmr_base + 0x38));
  83. readq_relaxed((void __iomem *)(mmr_base + 0xb050));
  84. }
  85. }
  86. /* load mmr contents into a variable */
  87. #define tioce_mmr_load(kern, mmrp, varp) do {\
  88. tioce_mmr_war_pre(kern, mmrp); \
  89. *(varp) = readq_relaxed(mmrp); \
  90. tioce_mmr_war_post(kern, mmrp); \
  91. } while (0)
  92. /* store variable contents into mmr */
  93. #define tioce_mmr_store(kern, mmrp, varp) do {\
  94. tioce_mmr_war_pre(kern, mmrp); \
  95. writeq(*varp, mmrp); \
  96. tioce_mmr_war_post(kern, mmrp); \
  97. } while (0)
  98. /* store immediate value into mmr */
  99. #define tioce_mmr_storei(kern, mmrp, val) do {\
  100. tioce_mmr_war_pre(kern, mmrp); \
  101. writeq(val, mmrp); \
  102. tioce_mmr_war_post(kern, mmrp); \
  103. } while (0)
  104. /* set bits (immediate value) into mmr */
  105. #define tioce_mmr_seti(kern, mmrp, bits) do {\
  106. u64 tmp; \
  107. tioce_mmr_load(kern, mmrp, &tmp); \
  108. tmp |= (bits); \
  109. tioce_mmr_store(kern, mmrp, &tmp); \
  110. } while (0)
  111. /* clear bits (immediate value) into mmr */
  112. #define tioce_mmr_clri(kern, mmrp, bits) do { \
  113. u64 tmp; \
  114. tioce_mmr_load(kern, mmrp, &tmp); \
  115. tmp &= ~(bits); \
  116. tioce_mmr_store(kern, mmrp, &tmp); \
  117. } while (0)
  118. /**
  119. * Bus address ranges for the 5 flavors of TIOCE DMA
  120. */
  121. #define TIOCE_D64_MIN 0x8000000000000000UL
  122. #define TIOCE_D64_MAX 0xffffffffffffffffUL
  123. #define TIOCE_D64_ADDR(a) ((a) >= TIOCE_D64_MIN)
  124. #define TIOCE_D32_MIN 0x0000000080000000UL
  125. #define TIOCE_D32_MAX 0x00000000ffffffffUL
  126. #define TIOCE_D32_ADDR(a) ((a) >= TIOCE_D32_MIN && (a) <= TIOCE_D32_MAX)
  127. #define TIOCE_M32_MIN 0x0000000000000000UL
  128. #define TIOCE_M32_MAX 0x000000007fffffffUL
  129. #define TIOCE_M32_ADDR(a) ((a) >= TIOCE_M32_MIN && (a) <= TIOCE_M32_MAX)
  130. #define TIOCE_M40_MIN 0x0000004000000000UL
  131. #define TIOCE_M40_MAX 0x0000007fffffffffUL
  132. #define TIOCE_M40_ADDR(a) ((a) >= TIOCE_M40_MIN && (a) <= TIOCE_M40_MAX)
  133. #define TIOCE_M40S_MIN 0x0000008000000000UL
  134. #define TIOCE_M40S_MAX 0x000000ffffffffffUL
  135. #define TIOCE_M40S_ADDR(a) ((a) >= TIOCE_M40S_MIN && (a) <= TIOCE_M40S_MAX)
  136. /*
  137. * ATE manipulation macros.
  138. */
  139. #define ATE_PAGESHIFT(ps) (__ffs(ps))
  140. #define ATE_PAGEMASK(ps) ((ps)-1)
  141. #define ATE_PAGE(x, ps) ((x) >> ATE_PAGESHIFT(ps))
  142. #define ATE_NPAGES(start, len, pagesize) \
  143. (ATE_PAGE((start)+(len)-1, pagesize) - ATE_PAGE(start, pagesize) + 1)
  144. #define ATE_VALID(ate) ((ate) & (1UL << 63))
  145. #define ATE_MAKE(addr, ps, msi) \
  146. (((addr) & ~ATE_PAGEMASK(ps)) | (1UL << 63) | ((msi)?(1UL << 62):0))
  147. /*
  148. * Flavors of ate-based mapping supported by tioce_alloc_map()
  149. */
  150. #define TIOCE_ATE_M32 1
  151. #define TIOCE_ATE_M40 2
  152. #define TIOCE_ATE_M40S 3
  153. #define KB(x) ((u64)(x) << 10)
  154. #define MB(x) ((u64)(x) << 20)
  155. #define GB(x) ((u64)(x) << 30)
  156. /**
  157. * tioce_dma_d64 - create a DMA mapping using 64-bit direct mode
  158. * @ct_addr: system coretalk address
  159. *
  160. * Map @ct_addr into 64-bit CE bus space. No device context is necessary
  161. * and no CE mapping are consumed.
  162. *
  163. * Bits 53:0 come from the coretalk address. The remaining bits are set as
  164. * follows:
  165. *
  166. * 63 - must be 1 to indicate d64 mode to CE hardware
  167. * 62 - barrier bit ... controlled with tioce_dma_barrier()
  168. * 61 - msi bit ... specified through dma_flags
  169. * 60:54 - reserved, MBZ
  170. */
  171. static u64
  172. tioce_dma_d64(unsigned long ct_addr, int dma_flags)
  173. {
  174. u64 bus_addr;
  175. bus_addr = ct_addr | (1UL << 63);
  176. if (dma_flags & SN_DMA_MSI)
  177. bus_addr |= (1UL << 61);
  178. return bus_addr;
  179. }
  180. /**
  181. * pcidev_to_tioce - return misc ce related pointers given a pci_dev
  182. * @pci_dev: pci device context
  183. * @base: ptr to store struct tioce_mmr * for the CE holding this device
  184. * @kernel: ptr to store struct tioce_kernel * for the CE holding this device
  185. * @port: ptr to store the CE port number that this device is on
  186. *
  187. * Return pointers to various CE-related structures for the CE upstream of
  188. * @pci_dev.
  189. */
  190. static inline void
  191. pcidev_to_tioce(struct pci_dev *pdev, struct tioce __iomem **base,
  192. struct tioce_kernel **kernel, int *port)
  193. {
  194. struct pcidev_info *pcidev_info;
  195. struct tioce_common *ce_common;
  196. struct tioce_kernel *ce_kernel;
  197. pcidev_info = SN_PCIDEV_INFO(pdev);
  198. ce_common = (struct tioce_common *)pcidev_info->pdi_pcibus_info;
  199. ce_kernel = (struct tioce_kernel *)ce_common->ce_kernel_private;
  200. if (base)
  201. *base = (struct tioce __iomem *)ce_common->ce_pcibus.bs_base;
  202. if (kernel)
  203. *kernel = ce_kernel;
  204. /*
  205. * we use port as a zero-based value internally, even though the
  206. * documentation is 1-based.
  207. */
  208. if (port)
  209. *port =
  210. (pdev->bus->number < ce_kernel->ce_port1_secondary) ? 0 : 1;
  211. }
  212. /**
  213. * tioce_alloc_map - Given a coretalk address, map it to pcie bus address
  214. * space using one of the various ATE-based address modes.
  215. * @ce_kern: tioce context
  216. * @type: map mode to use
  217. * @port: 0-based port that the requesting device is downstream of
  218. * @ct_addr: the coretalk address to map
  219. * @len: number of bytes to map
  220. *
  221. * Given the addressing type, set up various paramaters that define the
  222. * ATE pool to use. Search for a contiguous block of entries to cover the
  223. * length, and if enough resources exist, fill in the ATE's and construct a
  224. * tioce_dmamap struct to track the mapping.
  225. */
  226. static u64
  227. tioce_alloc_map(struct tioce_kernel *ce_kern, int type, int port,
  228. u64 ct_addr, int len, int dma_flags)
  229. {
  230. int i;
  231. int j;
  232. int first;
  233. int last;
  234. int entries;
  235. int nates;
  236. u64 pagesize;
  237. int msi_capable, msi_wanted;
  238. u64 *ate_shadow;
  239. u64 __iomem *ate_reg;
  240. u64 addr;
  241. struct tioce __iomem *ce_mmr;
  242. u64 bus_base;
  243. struct tioce_dmamap *map;
  244. ce_mmr = (struct tioce __iomem *)ce_kern->ce_common->ce_pcibus.bs_base;
  245. switch (type) {
  246. case TIOCE_ATE_M32:
  247. /*
  248. * The first 64 entries of the ate3240 pool are dedicated to
  249. * super-page (TIOCE_ATE_M40S) mode.
  250. */
  251. first = 64;
  252. entries = TIOCE_NUM_M3240_ATES - 64;
  253. ate_shadow = ce_kern->ce_ate3240_shadow;
  254. ate_reg = ce_mmr->ce_ure_ate3240;
  255. pagesize = ce_kern->ce_ate3240_pagesize;
  256. bus_base = TIOCE_M32_MIN;
  257. msi_capable = 1;
  258. break;
  259. case TIOCE_ATE_M40:
  260. first = 0;
  261. entries = TIOCE_NUM_M40_ATES;
  262. ate_shadow = ce_kern->ce_ate40_shadow;
  263. ate_reg = ce_mmr->ce_ure_ate40;
  264. pagesize = MB(64);
  265. bus_base = TIOCE_M40_MIN;
  266. msi_capable = 0;
  267. break;
  268. case TIOCE_ATE_M40S:
  269. /*
  270. * ate3240 entries 0-31 are dedicated to port1 super-page
  271. * mappings. ate3240 entries 32-63 are dedicated to port2.
  272. */
  273. first = port * 32;
  274. entries = 32;
  275. ate_shadow = ce_kern->ce_ate3240_shadow;
  276. ate_reg = ce_mmr->ce_ure_ate3240;
  277. pagesize = GB(16);
  278. bus_base = TIOCE_M40S_MIN;
  279. msi_capable = 0;
  280. break;
  281. default:
  282. return 0;
  283. }
  284. msi_wanted = dma_flags & SN_DMA_MSI;
  285. if (msi_wanted && !msi_capable)
  286. return 0;
  287. nates = ATE_NPAGES(ct_addr, len, pagesize);
  288. if (nates > entries)
  289. return 0;
  290. last = first + entries - nates;
  291. for (i = first; i <= last; i++) {
  292. if (ATE_VALID(ate_shadow[i]))
  293. continue;
  294. for (j = i; j < i + nates; j++)
  295. if (ATE_VALID(ate_shadow[j]))
  296. break;
  297. if (j >= i + nates)
  298. break;
  299. }
  300. if (i > last)
  301. return 0;
  302. map = kzalloc(sizeof(struct tioce_dmamap), GFP_ATOMIC);
  303. if (!map)
  304. return 0;
  305. addr = ct_addr;
  306. for (j = 0; j < nates; j++) {
  307. u64 ate;
  308. ate = ATE_MAKE(addr, pagesize, msi_wanted);
  309. ate_shadow[i + j] = ate;
  310. tioce_mmr_storei(ce_kern, &ate_reg[i + j], ate);
  311. addr += pagesize;
  312. }
  313. map->refcnt = 1;
  314. map->nbytes = nates * pagesize;
  315. map->ct_start = ct_addr & ~ATE_PAGEMASK(pagesize);
  316. map->pci_start = bus_base + (i * pagesize);
  317. map->ate_hw = &ate_reg[i];
  318. map->ate_shadow = &ate_shadow[i];
  319. map->ate_count = nates;
  320. list_add(&map->ce_dmamap_list, &ce_kern->ce_dmamap_list);
  321. return (map->pci_start + (ct_addr - map->ct_start));
  322. }
  323. /**
  324. * tioce_dma_d32 - create a DMA mapping using 32-bit direct mode
  325. * @pdev: linux pci_dev representing the function
  326. * @paddr: system physical address
  327. *
  328. * Map @paddr into 32-bit bus space of the CE associated with @pcidev_info.
  329. */
  330. static u64
  331. tioce_dma_d32(struct pci_dev *pdev, u64 ct_addr, int dma_flags)
  332. {
  333. int dma_ok;
  334. int port;
  335. struct tioce __iomem *ce_mmr;
  336. struct tioce_kernel *ce_kern;
  337. u64 ct_upper;
  338. u64 ct_lower;
  339. dma_addr_t bus_addr;
  340. if (dma_flags & SN_DMA_MSI)
  341. return 0;
  342. ct_upper = ct_addr & ~0x3fffffffUL;
  343. ct_lower = ct_addr & 0x3fffffffUL;
  344. pcidev_to_tioce(pdev, &ce_mmr, &ce_kern, &port);
  345. if (ce_kern->ce_port[port].dirmap_refcnt == 0) {
  346. u64 tmp;
  347. ce_kern->ce_port[port].dirmap_shadow = ct_upper;
  348. tioce_mmr_storei(ce_kern, &ce_mmr->ce_ure_dir_map[port],
  349. ct_upper);
  350. tmp = ce_mmr->ce_ure_dir_map[port];
  351. dma_ok = 1;
  352. } else
  353. dma_ok = (ce_kern->ce_port[port].dirmap_shadow == ct_upper);
  354. if (dma_ok) {
  355. ce_kern->ce_port[port].dirmap_refcnt++;
  356. bus_addr = TIOCE_D32_MIN + ct_lower;
  357. } else
  358. bus_addr = 0;
  359. return bus_addr;
  360. }
  361. /**
  362. * tioce_dma_barrier - swizzle a TIOCE bus address to include or exclude
  363. * the barrier bit.
  364. * @bus_addr: bus address to swizzle
  365. *
  366. * Given a TIOCE bus address, set the appropriate bit to indicate barrier
  367. * attributes.
  368. */
  369. static u64
  370. tioce_dma_barrier(u64 bus_addr, int on)
  371. {
  372. u64 barrier_bit;
  373. /* barrier not supported in M40/M40S mode */
  374. if (TIOCE_M40_ADDR(bus_addr) || TIOCE_M40S_ADDR(bus_addr))
  375. return bus_addr;
  376. if (TIOCE_D64_ADDR(bus_addr))
  377. barrier_bit = (1UL << 62);
  378. else /* must be m32 or d32 */
  379. barrier_bit = (1UL << 30);
  380. return (on) ? (bus_addr | barrier_bit) : (bus_addr & ~barrier_bit);
  381. }
  382. /**
  383. * tioce_dma_unmap - release CE mapping resources
  384. * @pdev: linux pci_dev representing the function
  385. * @bus_addr: bus address returned by an earlier tioce_dma_map
  386. * @dir: mapping direction (unused)
  387. *
  388. * Locate mapping resources associated with @bus_addr and release them.
  389. * For mappings created using the direct modes there are no resources
  390. * to release.
  391. */
  392. void
  393. tioce_dma_unmap(struct pci_dev *pdev, dma_addr_t bus_addr, int dir)
  394. {
  395. int i;
  396. int port;
  397. struct tioce_kernel *ce_kern;
  398. struct tioce __iomem *ce_mmr;
  399. unsigned long flags;
  400. bus_addr = tioce_dma_barrier(bus_addr, 0);
  401. pcidev_to_tioce(pdev, &ce_mmr, &ce_kern, &port);
  402. /* nothing to do for D64 */
  403. if (TIOCE_D64_ADDR(bus_addr))
  404. return;
  405. spin_lock_irqsave(&ce_kern->ce_lock, flags);
  406. if (TIOCE_D32_ADDR(bus_addr)) {
  407. if (--ce_kern->ce_port[port].dirmap_refcnt == 0) {
  408. ce_kern->ce_port[port].dirmap_shadow = 0;
  409. tioce_mmr_storei(ce_kern, &ce_mmr->ce_ure_dir_map[port],
  410. 0);
  411. }
  412. } else {
  413. struct tioce_dmamap *map;
  414. list_for_each_entry(map, &ce_kern->ce_dmamap_list,
  415. ce_dmamap_list) {
  416. u64 last;
  417. last = map->pci_start + map->nbytes - 1;
  418. if (bus_addr >= map->pci_start && bus_addr <= last)
  419. break;
  420. }
  421. if (&map->ce_dmamap_list == &ce_kern->ce_dmamap_list) {
  422. printk(KERN_WARNING
  423. "%s: %s - no map found for bus_addr 0x%lx\n",
  424. __FUNCTION__, pci_name(pdev), bus_addr);
  425. } else if (--map->refcnt == 0) {
  426. for (i = 0; i < map->ate_count; i++) {
  427. map->ate_shadow[i] = 0;
  428. tioce_mmr_storei(ce_kern, &map->ate_hw[i], 0);
  429. }
  430. list_del(&map->ce_dmamap_list);
  431. kfree(map);
  432. }
  433. }
  434. spin_unlock_irqrestore(&ce_kern->ce_lock, flags);
  435. }
  436. /**
  437. * tioce_do_dma_map - map pages for PCI DMA
  438. * @pdev: linux pci_dev representing the function
  439. * @paddr: host physical address to map
  440. * @byte_count: bytes to map
  441. *
  442. * This is the main wrapper for mapping host physical pages to CE PCI space.
  443. * The mapping mode used is based on the device's dma_mask.
  444. */
  445. static u64
  446. tioce_do_dma_map(struct pci_dev *pdev, u64 paddr, size_t byte_count,
  447. int barrier, int dma_flags)
  448. {
  449. unsigned long flags;
  450. u64 ct_addr;
  451. u64 mapaddr = 0;
  452. struct tioce_kernel *ce_kern;
  453. struct tioce_dmamap *map;
  454. int port;
  455. u64 dma_mask;
  456. dma_mask = (barrier) ? pdev->dev.coherent_dma_mask : pdev->dma_mask;
  457. /* cards must be able to address at least 31 bits */
  458. if (dma_mask < 0x7fffffffUL)
  459. return 0;
  460. if (SN_DMA_ADDRTYPE(dma_flags) == SN_DMA_ADDR_PHYS)
  461. ct_addr = PHYS_TO_TIODMA(paddr);
  462. else
  463. ct_addr = paddr;
  464. /*
  465. * If the device can generate 64 bit addresses, create a D64 map.
  466. */
  467. if (dma_mask == ~0UL) {
  468. mapaddr = tioce_dma_d64(ct_addr, dma_flags);
  469. if (mapaddr)
  470. goto dma_map_done;
  471. }
  472. pcidev_to_tioce(pdev, NULL, &ce_kern, &port);
  473. spin_lock_irqsave(&ce_kern->ce_lock, flags);
  474. /*
  475. * D64 didn't work ... See if we have an existing map that covers
  476. * this address range. Must account for devices dma_mask here since
  477. * an existing map might have been done in a mode using more pci
  478. * address bits than this device can support.
  479. */
  480. list_for_each_entry(map, &ce_kern->ce_dmamap_list, ce_dmamap_list) {
  481. u64 last;
  482. last = map->ct_start + map->nbytes - 1;
  483. if (ct_addr >= map->ct_start &&
  484. ct_addr + byte_count - 1 <= last &&
  485. map->pci_start <= dma_mask) {
  486. map->refcnt++;
  487. mapaddr = map->pci_start + (ct_addr - map->ct_start);
  488. break;
  489. }
  490. }
  491. /*
  492. * If we don't have a map yet, and the card can generate 40
  493. * bit addresses, try the M40/M40S modes. Note these modes do not
  494. * support a barrier bit, so if we need a consistent map these
  495. * won't work.
  496. */
  497. if (!mapaddr && !barrier && dma_mask >= 0xffffffffffUL) {
  498. /*
  499. * We have two options for 40-bit mappings: 16GB "super" ATE's
  500. * and 64MB "regular" ATE's. We'll try both if needed for a
  501. * given mapping but which one we try first depends on the
  502. * size. For requests >64MB, prefer to use a super page with
  503. * regular as the fallback. Otherwise, try in the reverse order.
  504. */
  505. if (byte_count > MB(64)) {
  506. mapaddr = tioce_alloc_map(ce_kern, TIOCE_ATE_M40S,
  507. port, ct_addr, byte_count,
  508. dma_flags);
  509. if (!mapaddr)
  510. mapaddr =
  511. tioce_alloc_map(ce_kern, TIOCE_ATE_M40, -1,
  512. ct_addr, byte_count,
  513. dma_flags);
  514. } else {
  515. mapaddr = tioce_alloc_map(ce_kern, TIOCE_ATE_M40, -1,
  516. ct_addr, byte_count,
  517. dma_flags);
  518. if (!mapaddr)
  519. mapaddr =
  520. tioce_alloc_map(ce_kern, TIOCE_ATE_M40S,
  521. port, ct_addr, byte_count,
  522. dma_flags);
  523. }
  524. }
  525. /*
  526. * 32-bit direct is the next mode to try
  527. */
  528. if (!mapaddr && dma_mask >= 0xffffffffUL)
  529. mapaddr = tioce_dma_d32(pdev, ct_addr, dma_flags);
  530. /*
  531. * Last resort, try 32-bit ATE-based map.
  532. */
  533. if (!mapaddr)
  534. mapaddr =
  535. tioce_alloc_map(ce_kern, TIOCE_ATE_M32, -1, ct_addr,
  536. byte_count, dma_flags);
  537. spin_unlock_irqrestore(&ce_kern->ce_lock, flags);
  538. dma_map_done:
  539. if (mapaddr && barrier)
  540. mapaddr = tioce_dma_barrier(mapaddr, 1);
  541. return mapaddr;
  542. }
  543. /**
  544. * tioce_dma - standard pci dma map interface
  545. * @pdev: pci device requesting the map
  546. * @paddr: system physical address to map into pci space
  547. * @byte_count: # bytes to map
  548. *
  549. * Simply call tioce_do_dma_map() to create a map with the barrier bit clear
  550. * in the address.
  551. */
  552. static u64
  553. tioce_dma(struct pci_dev *pdev, u64 paddr, size_t byte_count, int dma_flags)
  554. {
  555. return tioce_do_dma_map(pdev, paddr, byte_count, 0, dma_flags);
  556. }
  557. /**
  558. * tioce_dma_consistent - consistent pci dma map interface
  559. * @pdev: pci device requesting the map
  560. * @paddr: system physical address to map into pci space
  561. * @byte_count: # bytes to map
  562. *
  563. * Simply call tioce_do_dma_map() to create a map with the barrier bit set
  564. * in the address.
  565. */ static u64
  566. tioce_dma_consistent(struct pci_dev *pdev, u64 paddr, size_t byte_count, int dma_flags)
  567. {
  568. return tioce_do_dma_map(pdev, paddr, byte_count, 1, dma_flags);
  569. }
  570. /**
  571. * tioce_error_intr_handler - SGI TIO CE error interrupt handler
  572. * @irq: unused
  573. * @arg: pointer to tioce_common struct for the given CE
  574. *
  575. * Handle a CE error interrupt. Simply a wrapper around a SAL call which
  576. * defers processing to the SGI prom.
  577. */ static irqreturn_t
  578. tioce_error_intr_handler(int irq, void *arg)
  579. {
  580. struct tioce_common *soft = arg;
  581. struct ia64_sal_retval ret_stuff;
  582. ret_stuff.status = 0;
  583. ret_stuff.v0 = 0;
  584. SAL_CALL_NOLOCK(ret_stuff, (u64) SN_SAL_IOIF_ERROR_INTERRUPT,
  585. soft->ce_pcibus.bs_persist_segment,
  586. soft->ce_pcibus.bs_persist_busnum, 0, 0, 0, 0, 0);
  587. if (ret_stuff.v0)
  588. panic("tioce_error_intr_handler: Fatal TIOCE error");
  589. return IRQ_HANDLED;
  590. }
  591. /**
  592. * tioce_reserve_m32 - reserve M32 ate's for the indicated address range
  593. * @tioce_kernel: TIOCE context to reserve ate's for
  594. * @base: starting bus address to reserve
  595. * @limit: last bus address to reserve
  596. *
  597. * If base/limit falls within the range of bus space mapped through the
  598. * M32 space, reserve the resources corresponding to the range.
  599. */
  600. static void
  601. tioce_reserve_m32(struct tioce_kernel *ce_kern, u64 base, u64 limit)
  602. {
  603. int ate_index, last_ate, ps;
  604. struct tioce __iomem *ce_mmr;
  605. ce_mmr = (struct tioce __iomem *)ce_kern->ce_common->ce_pcibus.bs_base;
  606. ps = ce_kern->ce_ate3240_pagesize;
  607. ate_index = ATE_PAGE(base, ps);
  608. last_ate = ate_index + ATE_NPAGES(base, limit-base+1, ps) - 1;
  609. if (ate_index < 64)
  610. ate_index = 64;
  611. if (last_ate >= TIOCE_NUM_M3240_ATES)
  612. last_ate = TIOCE_NUM_M3240_ATES - 1;
  613. while (ate_index <= last_ate) {
  614. u64 ate;
  615. ate = ATE_MAKE(0xdeadbeef, ps, 0);
  616. ce_kern->ce_ate3240_shadow[ate_index] = ate;
  617. tioce_mmr_storei(ce_kern, &ce_mmr->ce_ure_ate3240[ate_index],
  618. ate);
  619. ate_index++;
  620. }
  621. }
  622. /**
  623. * tioce_kern_init - init kernel structures related to a given TIOCE
  624. * @tioce_common: ptr to a cached tioce_common struct that originated in prom
  625. */
  626. static struct tioce_kernel *
  627. tioce_kern_init(struct tioce_common *tioce_common)
  628. {
  629. int i;
  630. int ps;
  631. int dev;
  632. u32 tmp;
  633. unsigned int seg, bus;
  634. struct tioce __iomem *tioce_mmr;
  635. struct tioce_kernel *tioce_kern;
  636. tioce_kern = kzalloc(sizeof(struct tioce_kernel), GFP_KERNEL);
  637. if (!tioce_kern) {
  638. return NULL;
  639. }
  640. tioce_kern->ce_common = tioce_common;
  641. spin_lock_init(&tioce_kern->ce_lock);
  642. INIT_LIST_HEAD(&tioce_kern->ce_dmamap_list);
  643. tioce_common->ce_kernel_private = (u64) tioce_kern;
  644. /*
  645. * Determine the secondary bus number of the port2 logical PPB.
  646. * This is used to decide whether a given pci device resides on
  647. * port1 or port2. Note: We don't have enough plumbing set up
  648. * here to use pci_read_config_xxx() so use the raw_pci_ops vector.
  649. */
  650. seg = tioce_common->ce_pcibus.bs_persist_segment;
  651. bus = tioce_common->ce_pcibus.bs_persist_busnum;
  652. raw_pci_ops->read(seg, bus, PCI_DEVFN(2, 0), PCI_SECONDARY_BUS, 1,&tmp);
  653. tioce_kern->ce_port1_secondary = (u8) tmp;
  654. /*
  655. * Set PMU pagesize to the largest size available, and zero out
  656. * the ate's.
  657. */
  658. tioce_mmr = (struct tioce __iomem *)tioce_common->ce_pcibus.bs_base;
  659. tioce_mmr_clri(tioce_kern, &tioce_mmr->ce_ure_page_map,
  660. CE_URE_PAGESIZE_MASK);
  661. tioce_mmr_seti(tioce_kern, &tioce_mmr->ce_ure_page_map,
  662. CE_URE_256K_PAGESIZE);
  663. ps = tioce_kern->ce_ate3240_pagesize = KB(256);
  664. for (i = 0; i < TIOCE_NUM_M40_ATES; i++) {
  665. tioce_kern->ce_ate40_shadow[i] = 0;
  666. tioce_mmr_storei(tioce_kern, &tioce_mmr->ce_ure_ate40[i], 0);
  667. }
  668. for (i = 0; i < TIOCE_NUM_M3240_ATES; i++) {
  669. tioce_kern->ce_ate3240_shadow[i] = 0;
  670. tioce_mmr_storei(tioce_kern, &tioce_mmr->ce_ure_ate3240[i], 0);
  671. }
  672. /*
  673. * Reserve ATE's corresponding to reserved address ranges. These
  674. * include:
  675. *
  676. * Memory space covered by each PPB mem base/limit register
  677. * Memory space covered by each PPB prefetch base/limit register
  678. *
  679. * These bus ranges are for pio (downstream) traffic only, and so
  680. * cannot be used for DMA.
  681. */
  682. for (dev = 1; dev <= 2; dev++) {
  683. u64 base, limit;
  684. /* mem base/limit */
  685. raw_pci_ops->read(seg, bus, PCI_DEVFN(dev, 0),
  686. PCI_MEMORY_BASE, 2, &tmp);
  687. base = (u64)tmp << 16;
  688. raw_pci_ops->read(seg, bus, PCI_DEVFN(dev, 0),
  689. PCI_MEMORY_LIMIT, 2, &tmp);
  690. limit = (u64)tmp << 16;
  691. limit |= 0xfffffUL;
  692. if (base < limit)
  693. tioce_reserve_m32(tioce_kern, base, limit);
  694. /*
  695. * prefetch mem base/limit. The tioce ppb's have 64-bit
  696. * decoders, so read the upper portions w/o checking the
  697. * attributes.
  698. */
  699. raw_pci_ops->read(seg, bus, PCI_DEVFN(dev, 0),
  700. PCI_PREF_MEMORY_BASE, 2, &tmp);
  701. base = ((u64)tmp & PCI_PREF_RANGE_MASK) << 16;
  702. raw_pci_ops->read(seg, bus, PCI_DEVFN(dev, 0),
  703. PCI_PREF_BASE_UPPER32, 4, &tmp);
  704. base |= (u64)tmp << 32;
  705. raw_pci_ops->read(seg, bus, PCI_DEVFN(dev, 0),
  706. PCI_PREF_MEMORY_LIMIT, 2, &tmp);
  707. limit = ((u64)tmp & PCI_PREF_RANGE_MASK) << 16;
  708. limit |= 0xfffffUL;
  709. raw_pci_ops->read(seg, bus, PCI_DEVFN(dev, 0),
  710. PCI_PREF_LIMIT_UPPER32, 4, &tmp);
  711. limit |= (u64)tmp << 32;
  712. if ((base < limit) && TIOCE_M32_ADDR(base))
  713. tioce_reserve_m32(tioce_kern, base, limit);
  714. }
  715. return tioce_kern;
  716. }
  717. /**
  718. * tioce_force_interrupt - implement altix force_interrupt() backend for CE
  719. * @sn_irq_info: sn asic irq that we need an interrupt generated for
  720. *
  721. * Given an sn_irq_info struct, set the proper bit in ce_adm_force_int to
  722. * force a secondary interrupt to be generated. This is to work around an
  723. * asic issue where there is a small window of opportunity for a legacy device
  724. * interrupt to be lost.
  725. */
  726. static void
  727. tioce_force_interrupt(struct sn_irq_info *sn_irq_info)
  728. {
  729. struct pcidev_info *pcidev_info;
  730. struct tioce_common *ce_common;
  731. struct tioce_kernel *ce_kern;
  732. struct tioce __iomem *ce_mmr;
  733. u64 force_int_val;
  734. if (!sn_irq_info->irq_bridge)
  735. return;
  736. if (sn_irq_info->irq_bridge_type != PCIIO_ASIC_TYPE_TIOCE)
  737. return;
  738. pcidev_info = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;
  739. if (!pcidev_info)
  740. return;
  741. ce_common = (struct tioce_common *)pcidev_info->pdi_pcibus_info;
  742. ce_mmr = (struct tioce __iomem *)ce_common->ce_pcibus.bs_base;
  743. ce_kern = (struct tioce_kernel *)ce_common->ce_kernel_private;
  744. /*
  745. * TIOCE Rev A workaround (PV 945826), force an interrupt by writing
  746. * the TIO_INTx register directly (1/26/2006)
  747. */
  748. if (ce_common->ce_rev == TIOCE_REV_A) {
  749. u64 int_bit_mask = (1ULL << sn_irq_info->irq_int_bit);
  750. u64 status;
  751. tioce_mmr_load(ce_kern, &ce_mmr->ce_adm_int_status, &status);
  752. if (status & int_bit_mask) {
  753. u64 force_irq = (1 << 8) | sn_irq_info->irq_irq;
  754. u64 ctalk = sn_irq_info->irq_xtalkaddr;
  755. u64 nasid, offset;
  756. nasid = (ctalk & CTALK_NASID_MASK) >> CTALK_NASID_SHFT;
  757. offset = (ctalk & CTALK_NODE_OFFSET);
  758. HUB_S(TIO_IOSPACE_ADDR(nasid, offset), force_irq);
  759. }
  760. return;
  761. }
  762. /*
  763. * irq_int_bit is originally set up by prom, and holds the interrupt
  764. * bit shift (not mask) as defined by the bit definitions in the
  765. * ce_adm_int mmr. These shifts are not the same for the
  766. * ce_adm_force_int register, so do an explicit mapping here to make
  767. * things clearer.
  768. */
  769. switch (sn_irq_info->irq_int_bit) {
  770. case CE_ADM_INT_PCIE_PORT1_DEV_A_SHFT:
  771. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_A_SHFT;
  772. break;
  773. case CE_ADM_INT_PCIE_PORT1_DEV_B_SHFT:
  774. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_B_SHFT;
  775. break;
  776. case CE_ADM_INT_PCIE_PORT1_DEV_C_SHFT:
  777. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_C_SHFT;
  778. break;
  779. case CE_ADM_INT_PCIE_PORT1_DEV_D_SHFT:
  780. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_D_SHFT;
  781. break;
  782. case CE_ADM_INT_PCIE_PORT2_DEV_A_SHFT:
  783. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_A_SHFT;
  784. break;
  785. case CE_ADM_INT_PCIE_PORT2_DEV_B_SHFT:
  786. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_B_SHFT;
  787. break;
  788. case CE_ADM_INT_PCIE_PORT2_DEV_C_SHFT:
  789. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_C_SHFT;
  790. break;
  791. case CE_ADM_INT_PCIE_PORT2_DEV_D_SHFT:
  792. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_D_SHFT;
  793. break;
  794. default:
  795. return;
  796. }
  797. tioce_mmr_storei(ce_kern, &ce_mmr->ce_adm_force_int, force_int_val);
  798. }
  799. /**
  800. * tioce_target_interrupt - implement set_irq_affinity for tioce resident
  801. * functions. Note: only applies to line interrupts, not MSI's.
  802. *
  803. * @sn_irq_info: SN IRQ context
  804. *
  805. * Given an sn_irq_info, set the associated CE device's interrupt destination
  806. * register. Since the interrupt destination registers are on a per-ce-slot
  807. * basis, this will retarget line interrupts for all functions downstream of
  808. * the slot.
  809. */
  810. static void
  811. tioce_target_interrupt(struct sn_irq_info *sn_irq_info)
  812. {
  813. struct pcidev_info *pcidev_info;
  814. struct tioce_common *ce_common;
  815. struct tioce_kernel *ce_kern;
  816. struct tioce __iomem *ce_mmr;
  817. int bit;
  818. u64 vector;
  819. pcidev_info = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;
  820. if (!pcidev_info)
  821. return;
  822. ce_common = (struct tioce_common *)pcidev_info->pdi_pcibus_info;
  823. ce_mmr = (struct tioce __iomem *)ce_common->ce_pcibus.bs_base;
  824. ce_kern = (struct tioce_kernel *)ce_common->ce_kernel_private;
  825. bit = sn_irq_info->irq_int_bit;
  826. tioce_mmr_seti(ce_kern, &ce_mmr->ce_adm_int_mask, (1UL << bit));
  827. vector = (u64)sn_irq_info->irq_irq << INTR_VECTOR_SHFT;
  828. vector |= sn_irq_info->irq_xtalkaddr;
  829. tioce_mmr_storei(ce_kern, &ce_mmr->ce_adm_int_dest[bit], vector);
  830. tioce_mmr_clri(ce_kern, &ce_mmr->ce_adm_int_mask, (1UL << bit));
  831. tioce_force_interrupt(sn_irq_info);
  832. }
  833. /**
  834. * tioce_bus_fixup - perform final PCI fixup for a TIO CE bus
  835. * @prom_bussoft: Common prom/kernel struct representing the bus
  836. *
  837. * Replicates the tioce_common pointed to by @prom_bussoft in kernel
  838. * space. Allocates and initializes a kernel-only area for a given CE,
  839. * and sets up an irq for handling CE error interrupts.
  840. *
  841. * On successful setup, returns the kernel version of tioce_common back to
  842. * the caller.
  843. */
  844. static void *
  845. tioce_bus_fixup(struct pcibus_bussoft *prom_bussoft, struct pci_controller *controller)
  846. {
  847. struct tioce_common *tioce_common;
  848. struct tioce_kernel *tioce_kern;
  849. struct tioce __iomem *tioce_mmr;
  850. /*
  851. * Allocate kernel bus soft and copy from prom.
  852. */
  853. tioce_common = kzalloc(sizeof(struct tioce_common), GFP_KERNEL);
  854. if (!tioce_common)
  855. return NULL;
  856. memcpy(tioce_common, prom_bussoft, sizeof(struct tioce_common));
  857. tioce_common->ce_pcibus.bs_base |= __IA64_UNCACHED_OFFSET;
  858. tioce_kern = tioce_kern_init(tioce_common);
  859. if (tioce_kern == NULL) {
  860. kfree(tioce_common);
  861. return NULL;
  862. }
  863. /*
  864. * Clear out any transient errors before registering the error
  865. * interrupt handler.
  866. */
  867. tioce_mmr = (struct tioce __iomem *)tioce_common->ce_pcibus.bs_base;
  868. tioce_mmr_seti(tioce_kern, &tioce_mmr->ce_adm_int_status_alias, ~0ULL);
  869. tioce_mmr_seti(tioce_kern, &tioce_mmr->ce_adm_error_summary_alias,
  870. ~0ULL);
  871. tioce_mmr_seti(tioce_kern, &tioce_mmr->ce_dre_comp_err_addr, 0ULL);
  872. if (request_irq(SGI_PCIASIC_ERROR,
  873. tioce_error_intr_handler,
  874. IRQF_SHARED, "TIOCE error", (void *)tioce_common))
  875. printk(KERN_WARNING
  876. "%s: Unable to get irq %d. "
  877. "Error interrupts won't be routed for "
  878. "TIOCE bus %04x:%02x\n",
  879. __FUNCTION__, SGI_PCIASIC_ERROR,
  880. tioce_common->ce_pcibus.bs_persist_segment,
  881. tioce_common->ce_pcibus.bs_persist_busnum);
  882. return tioce_common;
  883. }
  884. static struct sn_pcibus_provider tioce_pci_interfaces = {
  885. .dma_map = tioce_dma,
  886. .dma_map_consistent = tioce_dma_consistent,
  887. .dma_unmap = tioce_dma_unmap,
  888. .bus_fixup = tioce_bus_fixup,
  889. .force_interrupt = tioce_force_interrupt,
  890. .target_interrupt = tioce_target_interrupt
  891. };
  892. /**
  893. * tioce_init_provider - init SN PCI provider ops for TIO CE
  894. */
  895. int
  896. tioce_init_provider(void)
  897. {
  898. sn_pci_provider[PCIIO_ASIC_TYPE_TIOCE] = &tioce_pci_interfaces;
  899. return 0;
  900. }