tioce_provider.c 29 KB

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