vio.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  1. /*
  2. * IBM PowerPC Virtual I/O Infrastructure Support.
  3. *
  4. * Copyright (c) 2003,2008 IBM Corp.
  5. * Dave Engebretsen engebret@us.ibm.com
  6. * Santiago Leon santil@us.ibm.com
  7. * Hollis Blanchard <hollisb@us.ibm.com>
  8. * Stephen Rothwell
  9. * Robert Jennings <rcjenn@us.ibm.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <linux/cpu.h>
  17. #include <linux/types.h>
  18. #include <linux/delay.h>
  19. #include <linux/stat.h>
  20. #include <linux/device.h>
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/console.h>
  24. #include <linux/export.h>
  25. #include <linux/mm.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/kobject.h>
  28. #include <asm/iommu.h>
  29. #include <asm/dma.h>
  30. #include <asm/vio.h>
  31. #include <asm/prom.h>
  32. #include <asm/firmware.h>
  33. #include <asm/tce.h>
  34. #include <asm/page.h>
  35. #include <asm/hvcall.h>
  36. static struct vio_dev vio_bus_device = { /* fake "parent" device */
  37. .name = "vio",
  38. .type = "",
  39. .dev.init_name = "vio",
  40. .dev.bus = &vio_bus_type,
  41. };
  42. #ifdef CONFIG_PPC_SMLPAR
  43. /**
  44. * vio_cmo_pool - A pool of IO memory for CMO use
  45. *
  46. * @size: The size of the pool in bytes
  47. * @free: The amount of free memory in the pool
  48. */
  49. struct vio_cmo_pool {
  50. size_t size;
  51. size_t free;
  52. };
  53. /* How many ms to delay queued balance work */
  54. #define VIO_CMO_BALANCE_DELAY 100
  55. /* Portion out IO memory to CMO devices by this chunk size */
  56. #define VIO_CMO_BALANCE_CHUNK 131072
  57. /**
  58. * vio_cmo_dev_entry - A device that is CMO-enabled and requires entitlement
  59. *
  60. * @vio_dev: struct vio_dev pointer
  61. * @list: pointer to other devices on bus that are being tracked
  62. */
  63. struct vio_cmo_dev_entry {
  64. struct vio_dev *viodev;
  65. struct list_head list;
  66. };
  67. /**
  68. * vio_cmo - VIO bus accounting structure for CMO entitlement
  69. *
  70. * @lock: spinlock for entire structure
  71. * @balance_q: work queue for balancing system entitlement
  72. * @device_list: list of CMO-enabled devices requiring entitlement
  73. * @entitled: total system entitlement in bytes
  74. * @reserve: pool of memory from which devices reserve entitlement, incl. spare
  75. * @excess: pool of excess entitlement not needed for device reserves or spare
  76. * @spare: IO memory for device hotplug functionality
  77. * @min: minimum necessary for system operation
  78. * @desired: desired memory for system operation
  79. * @curr: bytes currently allocated
  80. * @high: high water mark for IO data usage
  81. */
  82. static struct vio_cmo {
  83. spinlock_t lock;
  84. struct delayed_work balance_q;
  85. struct list_head device_list;
  86. size_t entitled;
  87. struct vio_cmo_pool reserve;
  88. struct vio_cmo_pool excess;
  89. size_t spare;
  90. size_t min;
  91. size_t desired;
  92. size_t curr;
  93. size_t high;
  94. } vio_cmo;
  95. /**
  96. * vio_cmo_OF_devices - Count the number of OF devices that have DMA windows
  97. */
  98. static int vio_cmo_num_OF_devs(void)
  99. {
  100. struct device_node *node_vroot;
  101. int count = 0;
  102. /*
  103. * Count the number of vdevice entries with an
  104. * ibm,my-dma-window OF property
  105. */
  106. node_vroot = of_find_node_by_name(NULL, "vdevice");
  107. if (node_vroot) {
  108. struct device_node *of_node;
  109. struct property *prop;
  110. for_each_child_of_node(node_vroot, of_node) {
  111. prop = of_find_property(of_node, "ibm,my-dma-window",
  112. NULL);
  113. if (prop)
  114. count++;
  115. }
  116. }
  117. of_node_put(node_vroot);
  118. return count;
  119. }
  120. /**
  121. * vio_cmo_alloc - allocate IO memory for CMO-enable devices
  122. *
  123. * @viodev: VIO device requesting IO memory
  124. * @size: size of allocation requested
  125. *
  126. * Allocations come from memory reserved for the devices and any excess
  127. * IO memory available to all devices. The spare pool used to service
  128. * hotplug must be equal to %VIO_CMO_MIN_ENT for the excess pool to be
  129. * made available.
  130. *
  131. * Return codes:
  132. * 0 for successful allocation and -ENOMEM for a failure
  133. */
  134. static inline int vio_cmo_alloc(struct vio_dev *viodev, size_t size)
  135. {
  136. unsigned long flags;
  137. size_t reserve_free = 0;
  138. size_t excess_free = 0;
  139. int ret = -ENOMEM;
  140. spin_lock_irqsave(&vio_cmo.lock, flags);
  141. /* Determine the amount of free entitlement available in reserve */
  142. if (viodev->cmo.entitled > viodev->cmo.allocated)
  143. reserve_free = viodev->cmo.entitled - viodev->cmo.allocated;
  144. /* If spare is not fulfilled, the excess pool can not be used. */
  145. if (vio_cmo.spare >= VIO_CMO_MIN_ENT)
  146. excess_free = vio_cmo.excess.free;
  147. /* The request can be satisfied */
  148. if ((reserve_free + excess_free) >= size) {
  149. vio_cmo.curr += size;
  150. if (vio_cmo.curr > vio_cmo.high)
  151. vio_cmo.high = vio_cmo.curr;
  152. viodev->cmo.allocated += size;
  153. size -= min(reserve_free, size);
  154. vio_cmo.excess.free -= size;
  155. ret = 0;
  156. }
  157. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  158. return ret;
  159. }
  160. /**
  161. * vio_cmo_dealloc - deallocate IO memory from CMO-enable devices
  162. * @viodev: VIO device freeing IO memory
  163. * @size: size of deallocation
  164. *
  165. * IO memory is freed by the device back to the correct memory pools.
  166. * The spare pool is replenished first from either memory pool, then
  167. * the reserve pool is used to reduce device entitlement, the excess
  168. * pool is used to increase the reserve pool toward the desired entitlement
  169. * target, and then the remaining memory is returned to the pools.
  170. *
  171. */
  172. static inline void vio_cmo_dealloc(struct vio_dev *viodev, size_t size)
  173. {
  174. unsigned long flags;
  175. size_t spare_needed = 0;
  176. size_t excess_freed = 0;
  177. size_t reserve_freed = size;
  178. size_t tmp;
  179. int balance = 0;
  180. spin_lock_irqsave(&vio_cmo.lock, flags);
  181. vio_cmo.curr -= size;
  182. /* Amount of memory freed from the excess pool */
  183. if (viodev->cmo.allocated > viodev->cmo.entitled) {
  184. excess_freed = min(reserve_freed, (viodev->cmo.allocated -
  185. viodev->cmo.entitled));
  186. reserve_freed -= excess_freed;
  187. }
  188. /* Remove allocation from device */
  189. viodev->cmo.allocated -= (reserve_freed + excess_freed);
  190. /* Spare is a subset of the reserve pool, replenish it first. */
  191. spare_needed = VIO_CMO_MIN_ENT - vio_cmo.spare;
  192. /*
  193. * Replenish the spare in the reserve pool from the excess pool.
  194. * This moves entitlement into the reserve pool.
  195. */
  196. if (spare_needed && excess_freed) {
  197. tmp = min(excess_freed, spare_needed);
  198. vio_cmo.excess.size -= tmp;
  199. vio_cmo.reserve.size += tmp;
  200. vio_cmo.spare += tmp;
  201. excess_freed -= tmp;
  202. spare_needed -= tmp;
  203. balance = 1;
  204. }
  205. /*
  206. * Replenish the spare in the reserve pool from the reserve pool.
  207. * This removes entitlement from the device down to VIO_CMO_MIN_ENT,
  208. * if needed, and gives it to the spare pool. The amount of used
  209. * memory in this pool does not change.
  210. */
  211. if (spare_needed && reserve_freed) {
  212. tmp = min3(spare_needed, reserve_freed, (viodev->cmo.entitled - VIO_CMO_MIN_ENT));
  213. vio_cmo.spare += tmp;
  214. viodev->cmo.entitled -= tmp;
  215. reserve_freed -= tmp;
  216. spare_needed -= tmp;
  217. balance = 1;
  218. }
  219. /*
  220. * Increase the reserve pool until the desired allocation is met.
  221. * Move an allocation freed from the excess pool into the reserve
  222. * pool and schedule a balance operation.
  223. */
  224. if (excess_freed && (vio_cmo.desired > vio_cmo.reserve.size)) {
  225. tmp = min(excess_freed, (vio_cmo.desired - vio_cmo.reserve.size));
  226. vio_cmo.excess.size -= tmp;
  227. vio_cmo.reserve.size += tmp;
  228. excess_freed -= tmp;
  229. balance = 1;
  230. }
  231. /* Return memory from the excess pool to that pool */
  232. if (excess_freed)
  233. vio_cmo.excess.free += excess_freed;
  234. if (balance)
  235. schedule_delayed_work(&vio_cmo.balance_q, VIO_CMO_BALANCE_DELAY);
  236. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  237. }
  238. /**
  239. * vio_cmo_entitlement_update - Manage system entitlement changes
  240. *
  241. * @new_entitlement: new system entitlement to attempt to accommodate
  242. *
  243. * Increases in entitlement will be used to fulfill the spare entitlement
  244. * and the rest is given to the excess pool. Decreases, if they are
  245. * possible, come from the excess pool and from unused device entitlement
  246. *
  247. * Returns: 0 on success, -ENOMEM when change can not be made
  248. */
  249. int vio_cmo_entitlement_update(size_t new_entitlement)
  250. {
  251. struct vio_dev *viodev;
  252. struct vio_cmo_dev_entry *dev_ent;
  253. unsigned long flags;
  254. size_t avail, delta, tmp;
  255. spin_lock_irqsave(&vio_cmo.lock, flags);
  256. /* Entitlement increases */
  257. if (new_entitlement > vio_cmo.entitled) {
  258. delta = new_entitlement - vio_cmo.entitled;
  259. /* Fulfill spare allocation */
  260. if (vio_cmo.spare < VIO_CMO_MIN_ENT) {
  261. tmp = min(delta, (VIO_CMO_MIN_ENT - vio_cmo.spare));
  262. vio_cmo.spare += tmp;
  263. vio_cmo.reserve.size += tmp;
  264. delta -= tmp;
  265. }
  266. /* Remaining new allocation goes to the excess pool */
  267. vio_cmo.entitled += delta;
  268. vio_cmo.excess.size += delta;
  269. vio_cmo.excess.free += delta;
  270. goto out;
  271. }
  272. /* Entitlement decreases */
  273. delta = vio_cmo.entitled - new_entitlement;
  274. avail = vio_cmo.excess.free;
  275. /*
  276. * Need to check how much unused entitlement each device can
  277. * sacrifice to fulfill entitlement change.
  278. */
  279. list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
  280. if (avail >= delta)
  281. break;
  282. viodev = dev_ent->viodev;
  283. if ((viodev->cmo.entitled > viodev->cmo.allocated) &&
  284. (viodev->cmo.entitled > VIO_CMO_MIN_ENT))
  285. avail += viodev->cmo.entitled -
  286. max_t(size_t, viodev->cmo.allocated,
  287. VIO_CMO_MIN_ENT);
  288. }
  289. if (delta <= avail) {
  290. vio_cmo.entitled -= delta;
  291. /* Take entitlement from the excess pool first */
  292. tmp = min(vio_cmo.excess.free, delta);
  293. vio_cmo.excess.size -= tmp;
  294. vio_cmo.excess.free -= tmp;
  295. delta -= tmp;
  296. /*
  297. * Remove all but VIO_CMO_MIN_ENT bytes from devices
  298. * until entitlement change is served
  299. */
  300. list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
  301. if (!delta)
  302. break;
  303. viodev = dev_ent->viodev;
  304. tmp = 0;
  305. if ((viodev->cmo.entitled > viodev->cmo.allocated) &&
  306. (viodev->cmo.entitled > VIO_CMO_MIN_ENT))
  307. tmp = viodev->cmo.entitled -
  308. max_t(size_t, viodev->cmo.allocated,
  309. VIO_CMO_MIN_ENT);
  310. viodev->cmo.entitled -= min(tmp, delta);
  311. delta -= min(tmp, delta);
  312. }
  313. } else {
  314. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  315. return -ENOMEM;
  316. }
  317. out:
  318. schedule_delayed_work(&vio_cmo.balance_q, 0);
  319. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  320. return 0;
  321. }
  322. /**
  323. * vio_cmo_balance - Balance entitlement among devices
  324. *
  325. * @work: work queue structure for this operation
  326. *
  327. * Any system entitlement above the minimum needed for devices, or
  328. * already allocated to devices, can be distributed to the devices.
  329. * The list of devices is iterated through to recalculate the desired
  330. * entitlement level and to determine how much entitlement above the
  331. * minimum entitlement is allocated to devices.
  332. *
  333. * Small chunks of the available entitlement are given to devices until
  334. * their requirements are fulfilled or there is no entitlement left to give.
  335. * Upon completion sizes of the reserve and excess pools are calculated.
  336. *
  337. * The system minimum entitlement level is also recalculated here.
  338. * Entitlement will be reserved for devices even after vio_bus_remove to
  339. * accommodate reloading the driver. The OF tree is walked to count the
  340. * number of devices present and this will remove entitlement for devices
  341. * that have actually left the system after having vio_bus_remove called.
  342. */
  343. static void vio_cmo_balance(struct work_struct *work)
  344. {
  345. struct vio_cmo *cmo;
  346. struct vio_dev *viodev;
  347. struct vio_cmo_dev_entry *dev_ent;
  348. unsigned long flags;
  349. size_t avail = 0, level, chunk, need;
  350. int devcount = 0, fulfilled;
  351. cmo = container_of(work, struct vio_cmo, balance_q.work);
  352. spin_lock_irqsave(&vio_cmo.lock, flags);
  353. /* Calculate minimum entitlement and fulfill spare */
  354. cmo->min = vio_cmo_num_OF_devs() * VIO_CMO_MIN_ENT;
  355. BUG_ON(cmo->min > cmo->entitled);
  356. cmo->spare = min_t(size_t, VIO_CMO_MIN_ENT, (cmo->entitled - cmo->min));
  357. cmo->min += cmo->spare;
  358. cmo->desired = cmo->min;
  359. /*
  360. * Determine how much entitlement is available and reset device
  361. * entitlements
  362. */
  363. avail = cmo->entitled - cmo->spare;
  364. list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
  365. viodev = dev_ent->viodev;
  366. devcount++;
  367. viodev->cmo.entitled = VIO_CMO_MIN_ENT;
  368. cmo->desired += (viodev->cmo.desired - VIO_CMO_MIN_ENT);
  369. avail -= max_t(size_t, viodev->cmo.allocated, VIO_CMO_MIN_ENT);
  370. }
  371. /*
  372. * Having provided each device with the minimum entitlement, loop
  373. * over the devices portioning out the remaining entitlement
  374. * until there is nothing left.
  375. */
  376. level = VIO_CMO_MIN_ENT;
  377. while (avail) {
  378. fulfilled = 0;
  379. list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
  380. viodev = dev_ent->viodev;
  381. if (viodev->cmo.desired <= level) {
  382. fulfilled++;
  383. continue;
  384. }
  385. /*
  386. * Give the device up to VIO_CMO_BALANCE_CHUNK
  387. * bytes of entitlement, but do not exceed the
  388. * desired level of entitlement for the device.
  389. */
  390. chunk = min_t(size_t, avail, VIO_CMO_BALANCE_CHUNK);
  391. chunk = min(chunk, (viodev->cmo.desired -
  392. viodev->cmo.entitled));
  393. viodev->cmo.entitled += chunk;
  394. /*
  395. * If the memory for this entitlement increase was
  396. * already allocated to the device it does not come
  397. * from the available pool being portioned out.
  398. */
  399. need = max(viodev->cmo.allocated, viodev->cmo.entitled)-
  400. max(viodev->cmo.allocated, level);
  401. avail -= need;
  402. }
  403. if (fulfilled == devcount)
  404. break;
  405. level += VIO_CMO_BALANCE_CHUNK;
  406. }
  407. /* Calculate new reserve and excess pool sizes */
  408. cmo->reserve.size = cmo->min;
  409. cmo->excess.free = 0;
  410. cmo->excess.size = 0;
  411. need = 0;
  412. list_for_each_entry(dev_ent, &vio_cmo.device_list, list) {
  413. viodev = dev_ent->viodev;
  414. /* Calculated reserve size above the minimum entitlement */
  415. if (viodev->cmo.entitled)
  416. cmo->reserve.size += (viodev->cmo.entitled -
  417. VIO_CMO_MIN_ENT);
  418. /* Calculated used excess entitlement */
  419. if (viodev->cmo.allocated > viodev->cmo.entitled)
  420. need += viodev->cmo.allocated - viodev->cmo.entitled;
  421. }
  422. cmo->excess.size = cmo->entitled - cmo->reserve.size;
  423. cmo->excess.free = cmo->excess.size - need;
  424. cancel_delayed_work(to_delayed_work(work));
  425. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  426. }
  427. static void *vio_dma_iommu_alloc_coherent(struct device *dev, size_t size,
  428. dma_addr_t *dma_handle, gfp_t flag,
  429. unsigned long attrs)
  430. {
  431. struct vio_dev *viodev = to_vio_dev(dev);
  432. void *ret;
  433. if (vio_cmo_alloc(viodev, roundup(size, PAGE_SIZE))) {
  434. atomic_inc(&viodev->cmo.allocs_failed);
  435. return NULL;
  436. }
  437. ret = dma_iommu_ops.alloc(dev, size, dma_handle, flag, attrs);
  438. if (unlikely(ret == NULL)) {
  439. vio_cmo_dealloc(viodev, roundup(size, PAGE_SIZE));
  440. atomic_inc(&viodev->cmo.allocs_failed);
  441. }
  442. return ret;
  443. }
  444. static void vio_dma_iommu_free_coherent(struct device *dev, size_t size,
  445. void *vaddr, dma_addr_t dma_handle,
  446. unsigned long attrs)
  447. {
  448. struct vio_dev *viodev = to_vio_dev(dev);
  449. dma_iommu_ops.free(dev, size, vaddr, dma_handle, attrs);
  450. vio_cmo_dealloc(viodev, roundup(size, PAGE_SIZE));
  451. }
  452. static dma_addr_t vio_dma_iommu_map_page(struct device *dev, struct page *page,
  453. unsigned long offset, size_t size,
  454. enum dma_data_direction direction,
  455. unsigned long attrs)
  456. {
  457. struct vio_dev *viodev = to_vio_dev(dev);
  458. struct iommu_table *tbl;
  459. dma_addr_t ret = IOMMU_MAPPING_ERROR;
  460. tbl = get_iommu_table_base(dev);
  461. if (vio_cmo_alloc(viodev, roundup(size, IOMMU_PAGE_SIZE(tbl)))) {
  462. atomic_inc(&viodev->cmo.allocs_failed);
  463. return ret;
  464. }
  465. ret = dma_iommu_ops.map_page(dev, page, offset, size, direction, attrs);
  466. if (unlikely(dma_mapping_error(dev, ret))) {
  467. vio_cmo_dealloc(viodev, roundup(size, IOMMU_PAGE_SIZE(tbl)));
  468. atomic_inc(&viodev->cmo.allocs_failed);
  469. }
  470. return ret;
  471. }
  472. static void vio_dma_iommu_unmap_page(struct device *dev, dma_addr_t dma_handle,
  473. size_t size,
  474. enum dma_data_direction direction,
  475. unsigned long attrs)
  476. {
  477. struct vio_dev *viodev = to_vio_dev(dev);
  478. struct iommu_table *tbl;
  479. tbl = get_iommu_table_base(dev);
  480. dma_iommu_ops.unmap_page(dev, dma_handle, size, direction, attrs);
  481. vio_cmo_dealloc(viodev, roundup(size, IOMMU_PAGE_SIZE(tbl)));
  482. }
  483. static int vio_dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,
  484. int nelems, enum dma_data_direction direction,
  485. unsigned long attrs)
  486. {
  487. struct vio_dev *viodev = to_vio_dev(dev);
  488. struct iommu_table *tbl;
  489. struct scatterlist *sgl;
  490. int ret, count;
  491. size_t alloc_size = 0;
  492. tbl = get_iommu_table_base(dev);
  493. for_each_sg(sglist, sgl, nelems, count)
  494. alloc_size += roundup(sgl->length, IOMMU_PAGE_SIZE(tbl));
  495. if (vio_cmo_alloc(viodev, alloc_size)) {
  496. atomic_inc(&viodev->cmo.allocs_failed);
  497. return 0;
  498. }
  499. ret = dma_iommu_ops.map_sg(dev, sglist, nelems, direction, attrs);
  500. if (unlikely(!ret)) {
  501. vio_cmo_dealloc(viodev, alloc_size);
  502. atomic_inc(&viodev->cmo.allocs_failed);
  503. return ret;
  504. }
  505. for_each_sg(sglist, sgl, ret, count)
  506. alloc_size -= roundup(sgl->dma_length, IOMMU_PAGE_SIZE(tbl));
  507. if (alloc_size)
  508. vio_cmo_dealloc(viodev, alloc_size);
  509. return ret;
  510. }
  511. static void vio_dma_iommu_unmap_sg(struct device *dev,
  512. struct scatterlist *sglist, int nelems,
  513. enum dma_data_direction direction,
  514. unsigned long attrs)
  515. {
  516. struct vio_dev *viodev = to_vio_dev(dev);
  517. struct iommu_table *tbl;
  518. struct scatterlist *sgl;
  519. size_t alloc_size = 0;
  520. int count;
  521. tbl = get_iommu_table_base(dev);
  522. for_each_sg(sglist, sgl, nelems, count)
  523. alloc_size += roundup(sgl->dma_length, IOMMU_PAGE_SIZE(tbl));
  524. dma_iommu_ops.unmap_sg(dev, sglist, nelems, direction, attrs);
  525. vio_cmo_dealloc(viodev, alloc_size);
  526. }
  527. static int vio_dma_iommu_dma_supported(struct device *dev, u64 mask)
  528. {
  529. return dma_iommu_ops.dma_supported(dev, mask);
  530. }
  531. static u64 vio_dma_get_required_mask(struct device *dev)
  532. {
  533. return dma_iommu_ops.get_required_mask(dev);
  534. }
  535. static const struct dma_map_ops vio_dma_mapping_ops = {
  536. .alloc = vio_dma_iommu_alloc_coherent,
  537. .free = vio_dma_iommu_free_coherent,
  538. .mmap = dma_nommu_mmap_coherent,
  539. .map_sg = vio_dma_iommu_map_sg,
  540. .unmap_sg = vio_dma_iommu_unmap_sg,
  541. .map_page = vio_dma_iommu_map_page,
  542. .unmap_page = vio_dma_iommu_unmap_page,
  543. .dma_supported = vio_dma_iommu_dma_supported,
  544. .get_required_mask = vio_dma_get_required_mask,
  545. .mapping_error = dma_iommu_mapping_error,
  546. };
  547. /**
  548. * vio_cmo_set_dev_desired - Set desired entitlement for a device
  549. *
  550. * @viodev: struct vio_dev for device to alter
  551. * @desired: new desired entitlement level in bytes
  552. *
  553. * For use by devices to request a change to their entitlement at runtime or
  554. * through sysfs. The desired entitlement level is changed and a balancing
  555. * of system resources is scheduled to run in the future.
  556. */
  557. void vio_cmo_set_dev_desired(struct vio_dev *viodev, size_t desired)
  558. {
  559. unsigned long flags;
  560. struct vio_cmo_dev_entry *dev_ent;
  561. int found = 0;
  562. if (!firmware_has_feature(FW_FEATURE_CMO))
  563. return;
  564. spin_lock_irqsave(&vio_cmo.lock, flags);
  565. if (desired < VIO_CMO_MIN_ENT)
  566. desired = VIO_CMO_MIN_ENT;
  567. /*
  568. * Changes will not be made for devices not in the device list.
  569. * If it is not in the device list, then no driver is loaded
  570. * for the device and it can not receive entitlement.
  571. */
  572. list_for_each_entry(dev_ent, &vio_cmo.device_list, list)
  573. if (viodev == dev_ent->viodev) {
  574. found = 1;
  575. break;
  576. }
  577. if (!found) {
  578. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  579. return;
  580. }
  581. /* Increase/decrease in desired device entitlement */
  582. if (desired >= viodev->cmo.desired) {
  583. /* Just bump the bus and device values prior to a balance*/
  584. vio_cmo.desired += desired - viodev->cmo.desired;
  585. viodev->cmo.desired = desired;
  586. } else {
  587. /* Decrease bus and device values for desired entitlement */
  588. vio_cmo.desired -= viodev->cmo.desired - desired;
  589. viodev->cmo.desired = desired;
  590. /*
  591. * If less entitlement is desired than current entitlement, move
  592. * any reserve memory in the change region to the excess pool.
  593. */
  594. if (viodev->cmo.entitled > desired) {
  595. vio_cmo.reserve.size -= viodev->cmo.entitled - desired;
  596. vio_cmo.excess.size += viodev->cmo.entitled - desired;
  597. /*
  598. * If entitlement moving from the reserve pool to the
  599. * excess pool is currently unused, add to the excess
  600. * free counter.
  601. */
  602. if (viodev->cmo.allocated < viodev->cmo.entitled)
  603. vio_cmo.excess.free += viodev->cmo.entitled -
  604. max(viodev->cmo.allocated, desired);
  605. viodev->cmo.entitled = desired;
  606. }
  607. }
  608. schedule_delayed_work(&vio_cmo.balance_q, 0);
  609. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  610. }
  611. /**
  612. * vio_cmo_bus_probe - Handle CMO specific bus probe activities
  613. *
  614. * @viodev - Pointer to struct vio_dev for device
  615. *
  616. * Determine the devices IO memory entitlement needs, attempting
  617. * to satisfy the system minimum entitlement at first and scheduling
  618. * a balance operation to take care of the rest at a later time.
  619. *
  620. * Returns: 0 on success, -EINVAL when device doesn't support CMO, and
  621. * -ENOMEM when entitlement is not available for device or
  622. * device entry.
  623. *
  624. */
  625. static int vio_cmo_bus_probe(struct vio_dev *viodev)
  626. {
  627. struct vio_cmo_dev_entry *dev_ent;
  628. struct device *dev = &viodev->dev;
  629. struct iommu_table *tbl;
  630. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  631. unsigned long flags;
  632. size_t size;
  633. bool dma_capable = false;
  634. tbl = get_iommu_table_base(dev);
  635. /* A device requires entitlement if it has a DMA window property */
  636. switch (viodev->family) {
  637. case VDEVICE:
  638. if (of_get_property(viodev->dev.of_node,
  639. "ibm,my-dma-window", NULL))
  640. dma_capable = true;
  641. break;
  642. case PFO:
  643. dma_capable = false;
  644. break;
  645. default:
  646. dev_warn(dev, "unknown device family: %d\n", viodev->family);
  647. BUG();
  648. break;
  649. }
  650. /* Configure entitlement for the device. */
  651. if (dma_capable) {
  652. /* Check that the driver is CMO enabled and get desired DMA */
  653. if (!viodrv->get_desired_dma) {
  654. dev_err(dev, "%s: device driver does not support CMO\n",
  655. __func__);
  656. return -EINVAL;
  657. }
  658. viodev->cmo.desired =
  659. IOMMU_PAGE_ALIGN(viodrv->get_desired_dma(viodev), tbl);
  660. if (viodev->cmo.desired < VIO_CMO_MIN_ENT)
  661. viodev->cmo.desired = VIO_CMO_MIN_ENT;
  662. size = VIO_CMO_MIN_ENT;
  663. dev_ent = kmalloc(sizeof(struct vio_cmo_dev_entry),
  664. GFP_KERNEL);
  665. if (!dev_ent)
  666. return -ENOMEM;
  667. dev_ent->viodev = viodev;
  668. spin_lock_irqsave(&vio_cmo.lock, flags);
  669. list_add(&dev_ent->list, &vio_cmo.device_list);
  670. } else {
  671. viodev->cmo.desired = 0;
  672. size = 0;
  673. spin_lock_irqsave(&vio_cmo.lock, flags);
  674. }
  675. /*
  676. * If the needs for vio_cmo.min have not changed since they
  677. * were last set, the number of devices in the OF tree has
  678. * been constant and the IO memory for this is already in
  679. * the reserve pool.
  680. */
  681. if (vio_cmo.min == ((vio_cmo_num_OF_devs() + 1) *
  682. VIO_CMO_MIN_ENT)) {
  683. /* Updated desired entitlement if device requires it */
  684. if (size)
  685. vio_cmo.desired += (viodev->cmo.desired -
  686. VIO_CMO_MIN_ENT);
  687. } else {
  688. size_t tmp;
  689. tmp = vio_cmo.spare + vio_cmo.excess.free;
  690. if (tmp < size) {
  691. dev_err(dev, "%s: insufficient free "
  692. "entitlement to add device. "
  693. "Need %lu, have %lu\n", __func__,
  694. size, (vio_cmo.spare + tmp));
  695. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  696. return -ENOMEM;
  697. }
  698. /* Use excess pool first to fulfill request */
  699. tmp = min(size, vio_cmo.excess.free);
  700. vio_cmo.excess.free -= tmp;
  701. vio_cmo.excess.size -= tmp;
  702. vio_cmo.reserve.size += tmp;
  703. /* Use spare if excess pool was insufficient */
  704. vio_cmo.spare -= size - tmp;
  705. /* Update bus accounting */
  706. vio_cmo.min += size;
  707. vio_cmo.desired += viodev->cmo.desired;
  708. }
  709. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  710. return 0;
  711. }
  712. /**
  713. * vio_cmo_bus_remove - Handle CMO specific bus removal activities
  714. *
  715. * @viodev - Pointer to struct vio_dev for device
  716. *
  717. * Remove the device from the cmo device list. The minimum entitlement
  718. * will be reserved for the device as long as it is in the system. The
  719. * rest of the entitlement the device had been allocated will be returned
  720. * to the system.
  721. */
  722. static void vio_cmo_bus_remove(struct vio_dev *viodev)
  723. {
  724. struct vio_cmo_dev_entry *dev_ent;
  725. unsigned long flags;
  726. size_t tmp;
  727. spin_lock_irqsave(&vio_cmo.lock, flags);
  728. if (viodev->cmo.allocated) {
  729. dev_err(&viodev->dev, "%s: device had %lu bytes of IO "
  730. "allocated after remove operation.\n",
  731. __func__, viodev->cmo.allocated);
  732. BUG();
  733. }
  734. /*
  735. * Remove the device from the device list being maintained for
  736. * CMO enabled devices.
  737. */
  738. list_for_each_entry(dev_ent, &vio_cmo.device_list, list)
  739. if (viodev == dev_ent->viodev) {
  740. list_del(&dev_ent->list);
  741. kfree(dev_ent);
  742. break;
  743. }
  744. /*
  745. * Devices may not require any entitlement and they do not need
  746. * to be processed. Otherwise, return the device's entitlement
  747. * back to the pools.
  748. */
  749. if (viodev->cmo.entitled) {
  750. /*
  751. * This device has not yet left the OF tree, it's
  752. * minimum entitlement remains in vio_cmo.min and
  753. * vio_cmo.desired
  754. */
  755. vio_cmo.desired -= (viodev->cmo.desired - VIO_CMO_MIN_ENT);
  756. /*
  757. * Save min allocation for device in reserve as long
  758. * as it exists in OF tree as determined by later
  759. * balance operation
  760. */
  761. viodev->cmo.entitled -= VIO_CMO_MIN_ENT;
  762. /* Replenish spare from freed reserve pool */
  763. if (viodev->cmo.entitled && (vio_cmo.spare < VIO_CMO_MIN_ENT)) {
  764. tmp = min(viodev->cmo.entitled, (VIO_CMO_MIN_ENT -
  765. vio_cmo.spare));
  766. vio_cmo.spare += tmp;
  767. viodev->cmo.entitled -= tmp;
  768. }
  769. /* Remaining reserve goes to excess pool */
  770. vio_cmo.excess.size += viodev->cmo.entitled;
  771. vio_cmo.excess.free += viodev->cmo.entitled;
  772. vio_cmo.reserve.size -= viodev->cmo.entitled;
  773. /*
  774. * Until the device is removed it will keep a
  775. * minimum entitlement; this will guarantee that
  776. * a module unload/load will result in a success.
  777. */
  778. viodev->cmo.entitled = VIO_CMO_MIN_ENT;
  779. viodev->cmo.desired = VIO_CMO_MIN_ENT;
  780. atomic_set(&viodev->cmo.allocs_failed, 0);
  781. }
  782. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  783. }
  784. static void vio_cmo_set_dma_ops(struct vio_dev *viodev)
  785. {
  786. set_dma_ops(&viodev->dev, &vio_dma_mapping_ops);
  787. }
  788. /**
  789. * vio_cmo_bus_init - CMO entitlement initialization at bus init time
  790. *
  791. * Set up the reserve and excess entitlement pools based on available
  792. * system entitlement and the number of devices in the OF tree that
  793. * require entitlement in the reserve pool.
  794. */
  795. static void vio_cmo_bus_init(void)
  796. {
  797. struct hvcall_mpp_data mpp_data;
  798. int err;
  799. memset(&vio_cmo, 0, sizeof(struct vio_cmo));
  800. spin_lock_init(&vio_cmo.lock);
  801. INIT_LIST_HEAD(&vio_cmo.device_list);
  802. INIT_DELAYED_WORK(&vio_cmo.balance_q, vio_cmo_balance);
  803. /* Get current system entitlement */
  804. err = h_get_mpp(&mpp_data);
  805. /*
  806. * On failure, continue with entitlement set to 0, will panic()
  807. * later when spare is reserved.
  808. */
  809. if (err != H_SUCCESS) {
  810. printk(KERN_ERR "%s: unable to determine system IO "\
  811. "entitlement. (%d)\n", __func__, err);
  812. vio_cmo.entitled = 0;
  813. } else {
  814. vio_cmo.entitled = mpp_data.entitled_mem;
  815. }
  816. /* Set reservation and check against entitlement */
  817. vio_cmo.spare = VIO_CMO_MIN_ENT;
  818. vio_cmo.reserve.size = vio_cmo.spare;
  819. vio_cmo.reserve.size += (vio_cmo_num_OF_devs() *
  820. VIO_CMO_MIN_ENT);
  821. if (vio_cmo.reserve.size > vio_cmo.entitled) {
  822. printk(KERN_ERR "%s: insufficient system entitlement\n",
  823. __func__);
  824. panic("%s: Insufficient system entitlement", __func__);
  825. }
  826. /* Set the remaining accounting variables */
  827. vio_cmo.excess.size = vio_cmo.entitled - vio_cmo.reserve.size;
  828. vio_cmo.excess.free = vio_cmo.excess.size;
  829. vio_cmo.min = vio_cmo.reserve.size;
  830. vio_cmo.desired = vio_cmo.reserve.size;
  831. }
  832. /* sysfs device functions and data structures for CMO */
  833. #define viodev_cmo_rd_attr(name) \
  834. static ssize_t cmo_##name##_show(struct device *dev, \
  835. struct device_attribute *attr, \
  836. char *buf) \
  837. { \
  838. return sprintf(buf, "%lu\n", to_vio_dev(dev)->cmo.name); \
  839. }
  840. static ssize_t cmo_allocs_failed_show(struct device *dev,
  841. struct device_attribute *attr, char *buf)
  842. {
  843. struct vio_dev *viodev = to_vio_dev(dev);
  844. return sprintf(buf, "%d\n", atomic_read(&viodev->cmo.allocs_failed));
  845. }
  846. static ssize_t cmo_allocs_failed_store(struct device *dev,
  847. struct device_attribute *attr, const char *buf, size_t count)
  848. {
  849. struct vio_dev *viodev = to_vio_dev(dev);
  850. atomic_set(&viodev->cmo.allocs_failed, 0);
  851. return count;
  852. }
  853. static ssize_t cmo_desired_store(struct device *dev,
  854. struct device_attribute *attr, const char *buf, size_t count)
  855. {
  856. struct vio_dev *viodev = to_vio_dev(dev);
  857. size_t new_desired;
  858. int ret;
  859. ret = kstrtoul(buf, 10, &new_desired);
  860. if (ret)
  861. return ret;
  862. vio_cmo_set_dev_desired(viodev, new_desired);
  863. return count;
  864. }
  865. viodev_cmo_rd_attr(desired);
  866. viodev_cmo_rd_attr(entitled);
  867. viodev_cmo_rd_attr(allocated);
  868. static ssize_t name_show(struct device *, struct device_attribute *, char *);
  869. static ssize_t devspec_show(struct device *, struct device_attribute *, char *);
  870. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  871. char *buf);
  872. static struct device_attribute dev_attr_name;
  873. static struct device_attribute dev_attr_devspec;
  874. static struct device_attribute dev_attr_modalias;
  875. static DEVICE_ATTR_RO(cmo_entitled);
  876. static DEVICE_ATTR_RO(cmo_allocated);
  877. static DEVICE_ATTR_RW(cmo_desired);
  878. static DEVICE_ATTR_RW(cmo_allocs_failed);
  879. static struct attribute *vio_cmo_dev_attrs[] = {
  880. &dev_attr_name.attr,
  881. &dev_attr_devspec.attr,
  882. &dev_attr_modalias.attr,
  883. &dev_attr_cmo_entitled.attr,
  884. &dev_attr_cmo_allocated.attr,
  885. &dev_attr_cmo_desired.attr,
  886. &dev_attr_cmo_allocs_failed.attr,
  887. NULL,
  888. };
  889. ATTRIBUTE_GROUPS(vio_cmo_dev);
  890. /* sysfs bus functions and data structures for CMO */
  891. #define viobus_cmo_rd_attr(name) \
  892. static ssize_t cmo_bus_##name##_show(struct bus_type *bt, char *buf) \
  893. { \
  894. return sprintf(buf, "%lu\n", vio_cmo.name); \
  895. } \
  896. static struct bus_attribute bus_attr_cmo_bus_##name = \
  897. __ATTR(cmo_##name, S_IRUGO, cmo_bus_##name##_show, NULL)
  898. #define viobus_cmo_pool_rd_attr(name, var) \
  899. static ssize_t \
  900. cmo_##name##_##var##_show(struct bus_type *bt, char *buf) \
  901. { \
  902. return sprintf(buf, "%lu\n", vio_cmo.name.var); \
  903. } \
  904. static BUS_ATTR_RO(cmo_##name##_##var)
  905. viobus_cmo_rd_attr(entitled);
  906. viobus_cmo_rd_attr(spare);
  907. viobus_cmo_rd_attr(min);
  908. viobus_cmo_rd_attr(desired);
  909. viobus_cmo_rd_attr(curr);
  910. viobus_cmo_pool_rd_attr(reserve, size);
  911. viobus_cmo_pool_rd_attr(excess, size);
  912. viobus_cmo_pool_rd_attr(excess, free);
  913. static ssize_t cmo_high_show(struct bus_type *bt, char *buf)
  914. {
  915. return sprintf(buf, "%lu\n", vio_cmo.high);
  916. }
  917. static ssize_t cmo_high_store(struct bus_type *bt, const char *buf,
  918. size_t count)
  919. {
  920. unsigned long flags;
  921. spin_lock_irqsave(&vio_cmo.lock, flags);
  922. vio_cmo.high = vio_cmo.curr;
  923. spin_unlock_irqrestore(&vio_cmo.lock, flags);
  924. return count;
  925. }
  926. static BUS_ATTR_RW(cmo_high);
  927. static struct attribute *vio_bus_attrs[] = {
  928. &bus_attr_cmo_bus_entitled.attr,
  929. &bus_attr_cmo_bus_spare.attr,
  930. &bus_attr_cmo_bus_min.attr,
  931. &bus_attr_cmo_bus_desired.attr,
  932. &bus_attr_cmo_bus_curr.attr,
  933. &bus_attr_cmo_high.attr,
  934. &bus_attr_cmo_reserve_size.attr,
  935. &bus_attr_cmo_excess_size.attr,
  936. &bus_attr_cmo_excess_free.attr,
  937. NULL,
  938. };
  939. ATTRIBUTE_GROUPS(vio_bus);
  940. static void vio_cmo_sysfs_init(void)
  941. {
  942. vio_bus_type.dev_groups = vio_cmo_dev_groups;
  943. vio_bus_type.bus_groups = vio_bus_groups;
  944. }
  945. #else /* CONFIG_PPC_SMLPAR */
  946. int vio_cmo_entitlement_update(size_t new_entitlement) { return 0; }
  947. void vio_cmo_set_dev_desired(struct vio_dev *viodev, size_t desired) {}
  948. static int vio_cmo_bus_probe(struct vio_dev *viodev) { return 0; }
  949. static void vio_cmo_bus_remove(struct vio_dev *viodev) {}
  950. static void vio_cmo_set_dma_ops(struct vio_dev *viodev) {}
  951. static void vio_cmo_bus_init(void) {}
  952. static void vio_cmo_sysfs_init(void) { }
  953. #endif /* CONFIG_PPC_SMLPAR */
  954. EXPORT_SYMBOL(vio_cmo_entitlement_update);
  955. EXPORT_SYMBOL(vio_cmo_set_dev_desired);
  956. /*
  957. * Platform Facilities Option (PFO) support
  958. */
  959. /**
  960. * vio_h_cop_sync - Perform a synchronous PFO co-processor operation
  961. *
  962. * @vdev - Pointer to a struct vio_dev for device
  963. * @op - Pointer to a struct vio_pfo_op for the operation parameters
  964. *
  965. * Calls the hypervisor to synchronously perform the PFO operation
  966. * described in @op. In the case of a busy response from the hypervisor,
  967. * the operation will be re-submitted indefinitely unless a non-zero timeout
  968. * is specified or an error occurs. The timeout places a limit on when to
  969. * stop re-submitting a operation, the total time can be exceeded if an
  970. * operation is in progress.
  971. *
  972. * If op->hcall_ret is not NULL, this will be set to the return from the
  973. * last h_cop_op call or it will be 0 if an error not involving the h_call
  974. * was encountered.
  975. *
  976. * Returns:
  977. * 0 on success,
  978. * -EINVAL if the h_call fails due to an invalid parameter,
  979. * -E2BIG if the h_call can not be performed synchronously,
  980. * -EBUSY if a timeout is specified and has elapsed,
  981. * -EACCES if the memory area for data/status has been rescinded, or
  982. * -EPERM if a hardware fault has been indicated
  983. */
  984. int vio_h_cop_sync(struct vio_dev *vdev, struct vio_pfo_op *op)
  985. {
  986. struct device *dev = &vdev->dev;
  987. unsigned long deadline = 0;
  988. long hret = 0;
  989. int ret = 0;
  990. if (op->timeout)
  991. deadline = jiffies + msecs_to_jiffies(op->timeout);
  992. while (true) {
  993. hret = plpar_hcall_norets(H_COP, op->flags,
  994. vdev->resource_id,
  995. op->in, op->inlen, op->out,
  996. op->outlen, op->csbcpb);
  997. if (hret == H_SUCCESS ||
  998. (hret != H_NOT_ENOUGH_RESOURCES &&
  999. hret != H_BUSY && hret != H_RESOURCE) ||
  1000. (op->timeout && time_after(deadline, jiffies)))
  1001. break;
  1002. dev_dbg(dev, "%s: hcall ret(%ld), retrying.\n", __func__, hret);
  1003. }
  1004. switch (hret) {
  1005. case H_SUCCESS:
  1006. ret = 0;
  1007. break;
  1008. case H_OP_MODE:
  1009. case H_TOO_BIG:
  1010. ret = -E2BIG;
  1011. break;
  1012. case H_RESCINDED:
  1013. ret = -EACCES;
  1014. break;
  1015. case H_HARDWARE:
  1016. ret = -EPERM;
  1017. break;
  1018. case H_NOT_ENOUGH_RESOURCES:
  1019. case H_RESOURCE:
  1020. case H_BUSY:
  1021. ret = -EBUSY;
  1022. break;
  1023. default:
  1024. ret = -EINVAL;
  1025. break;
  1026. }
  1027. if (ret)
  1028. dev_dbg(dev, "%s: Sync h_cop_op failure (ret:%d) (hret:%ld)\n",
  1029. __func__, ret, hret);
  1030. op->hcall_err = hret;
  1031. return ret;
  1032. }
  1033. EXPORT_SYMBOL(vio_h_cop_sync);
  1034. static struct iommu_table *vio_build_iommu_table(struct vio_dev *dev)
  1035. {
  1036. const __be32 *dma_window;
  1037. struct iommu_table *tbl;
  1038. unsigned long offset, size;
  1039. dma_window = of_get_property(dev->dev.of_node,
  1040. "ibm,my-dma-window", NULL);
  1041. if (!dma_window)
  1042. return NULL;
  1043. tbl = kzalloc(sizeof(*tbl), GFP_KERNEL);
  1044. if (tbl == NULL)
  1045. return NULL;
  1046. of_parse_dma_window(dev->dev.of_node, dma_window,
  1047. &tbl->it_index, &offset, &size);
  1048. /* TCE table size - measured in tce entries */
  1049. tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
  1050. tbl->it_size = size >> tbl->it_page_shift;
  1051. /* offset for VIO should always be 0 */
  1052. tbl->it_offset = offset >> tbl->it_page_shift;
  1053. tbl->it_busno = 0;
  1054. tbl->it_type = TCE_VB;
  1055. tbl->it_blocksize = 16;
  1056. if (firmware_has_feature(FW_FEATURE_LPAR))
  1057. tbl->it_ops = &iommu_table_lpar_multi_ops;
  1058. else
  1059. tbl->it_ops = &iommu_table_pseries_ops;
  1060. return iommu_init_table(tbl, -1);
  1061. }
  1062. /**
  1063. * vio_match_device: - Tell if a VIO device has a matching
  1064. * VIO device id structure.
  1065. * @ids: array of VIO device id structures to search in
  1066. * @dev: the VIO device structure to match against
  1067. *
  1068. * Used by a driver to check whether a VIO device present in the
  1069. * system is in its list of supported devices. Returns the matching
  1070. * vio_device_id structure or NULL if there is no match.
  1071. */
  1072. static const struct vio_device_id *vio_match_device(
  1073. const struct vio_device_id *ids, const struct vio_dev *dev)
  1074. {
  1075. while (ids->type[0] != '\0') {
  1076. if ((strncmp(dev->type, ids->type, strlen(ids->type)) == 0) &&
  1077. of_device_is_compatible(dev->dev.of_node,
  1078. ids->compat))
  1079. return ids;
  1080. ids++;
  1081. }
  1082. return NULL;
  1083. }
  1084. /*
  1085. * Convert from struct device to struct vio_dev and pass to driver.
  1086. * dev->driver has already been set by generic code because vio_bus_match
  1087. * succeeded.
  1088. */
  1089. static int vio_bus_probe(struct device *dev)
  1090. {
  1091. struct vio_dev *viodev = to_vio_dev(dev);
  1092. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  1093. const struct vio_device_id *id;
  1094. int error = -ENODEV;
  1095. if (!viodrv->probe)
  1096. return error;
  1097. id = vio_match_device(viodrv->id_table, viodev);
  1098. if (id) {
  1099. memset(&viodev->cmo, 0, sizeof(viodev->cmo));
  1100. if (firmware_has_feature(FW_FEATURE_CMO)) {
  1101. error = vio_cmo_bus_probe(viodev);
  1102. if (error)
  1103. return error;
  1104. }
  1105. error = viodrv->probe(viodev, id);
  1106. if (error && firmware_has_feature(FW_FEATURE_CMO))
  1107. vio_cmo_bus_remove(viodev);
  1108. }
  1109. return error;
  1110. }
  1111. /* convert from struct device to struct vio_dev and pass to driver. */
  1112. static int vio_bus_remove(struct device *dev)
  1113. {
  1114. struct vio_dev *viodev = to_vio_dev(dev);
  1115. struct vio_driver *viodrv = to_vio_driver(dev->driver);
  1116. struct device *devptr;
  1117. int ret = 1;
  1118. /*
  1119. * Hold a reference to the device after the remove function is called
  1120. * to allow for CMO accounting cleanup for the device.
  1121. */
  1122. devptr = get_device(dev);
  1123. if (viodrv->remove)
  1124. ret = viodrv->remove(viodev);
  1125. if (!ret && firmware_has_feature(FW_FEATURE_CMO))
  1126. vio_cmo_bus_remove(viodev);
  1127. put_device(devptr);
  1128. return ret;
  1129. }
  1130. /**
  1131. * vio_register_driver: - Register a new vio driver
  1132. * @viodrv: The vio_driver structure to be registered.
  1133. */
  1134. int __vio_register_driver(struct vio_driver *viodrv, struct module *owner,
  1135. const char *mod_name)
  1136. {
  1137. pr_debug("%s: driver %s registering\n", __func__, viodrv->name);
  1138. /* fill in 'struct driver' fields */
  1139. viodrv->driver.name = viodrv->name;
  1140. viodrv->driver.pm = viodrv->pm;
  1141. viodrv->driver.bus = &vio_bus_type;
  1142. viodrv->driver.owner = owner;
  1143. viodrv->driver.mod_name = mod_name;
  1144. return driver_register(&viodrv->driver);
  1145. }
  1146. EXPORT_SYMBOL(__vio_register_driver);
  1147. /**
  1148. * vio_unregister_driver - Remove registration of vio driver.
  1149. * @viodrv: The vio_driver struct to be removed form registration
  1150. */
  1151. void vio_unregister_driver(struct vio_driver *viodrv)
  1152. {
  1153. driver_unregister(&viodrv->driver);
  1154. }
  1155. EXPORT_SYMBOL(vio_unregister_driver);
  1156. /* vio_dev refcount hit 0 */
  1157. static void vio_dev_release(struct device *dev)
  1158. {
  1159. struct iommu_table *tbl = get_iommu_table_base(dev);
  1160. if (tbl)
  1161. iommu_tce_table_put(tbl);
  1162. of_node_put(dev->of_node);
  1163. kfree(to_vio_dev(dev));
  1164. }
  1165. /**
  1166. * vio_register_device_node: - Register a new vio device.
  1167. * @of_node: The OF node for this device.
  1168. *
  1169. * Creates and initializes a vio_dev structure from the data in
  1170. * of_node and adds it to the list of virtual devices.
  1171. * Returns a pointer to the created vio_dev or NULL if node has
  1172. * NULL device_type or compatible fields.
  1173. */
  1174. struct vio_dev *vio_register_device_node(struct device_node *of_node)
  1175. {
  1176. struct vio_dev *viodev;
  1177. struct device_node *parent_node;
  1178. const __be32 *prop;
  1179. enum vio_dev_family family;
  1180. /*
  1181. * Determine if this node is a under the /vdevice node or under the
  1182. * /ibm,platform-facilities node. This decides the device's family.
  1183. */
  1184. parent_node = of_get_parent(of_node);
  1185. if (parent_node) {
  1186. if (!strcmp(parent_node->type, "ibm,platform-facilities"))
  1187. family = PFO;
  1188. else if (!strcmp(parent_node->type, "vdevice"))
  1189. family = VDEVICE;
  1190. else {
  1191. pr_warn("%s: parent(%pOF) of %pOFn not recognized.\n",
  1192. __func__,
  1193. parent_node,
  1194. of_node);
  1195. of_node_put(parent_node);
  1196. return NULL;
  1197. }
  1198. of_node_put(parent_node);
  1199. } else {
  1200. pr_warn("%s: could not determine the parent of node %pOFn.\n",
  1201. __func__, of_node);
  1202. return NULL;
  1203. }
  1204. if (family == PFO) {
  1205. if (of_get_property(of_node, "interrupt-controller", NULL)) {
  1206. pr_debug("%s: Skipping the interrupt controller %pOFn.\n",
  1207. __func__, of_node);
  1208. return NULL;
  1209. }
  1210. }
  1211. /* allocate a vio_dev for this node */
  1212. viodev = kzalloc(sizeof(struct vio_dev), GFP_KERNEL);
  1213. if (viodev == NULL) {
  1214. pr_warn("%s: allocation failure for VIO device.\n", __func__);
  1215. return NULL;
  1216. }
  1217. /* we need the 'device_type' property, in order to match with drivers */
  1218. viodev->family = family;
  1219. if (viodev->family == VDEVICE) {
  1220. unsigned int unit_address;
  1221. if (of_node->type != NULL)
  1222. viodev->type = of_node->type;
  1223. else {
  1224. pr_warn("%s: node %pOFn is missing the 'device_type' "
  1225. "property.\n", __func__, of_node);
  1226. goto out;
  1227. }
  1228. prop = of_get_property(of_node, "reg", NULL);
  1229. if (prop == NULL) {
  1230. pr_warn("%s: node %pOFn missing 'reg'\n",
  1231. __func__, of_node);
  1232. goto out;
  1233. }
  1234. unit_address = of_read_number(prop, 1);
  1235. dev_set_name(&viodev->dev, "%x", unit_address);
  1236. viodev->irq = irq_of_parse_and_map(of_node, 0);
  1237. viodev->unit_address = unit_address;
  1238. } else {
  1239. /* PFO devices need their resource_id for submitting COP_OPs
  1240. * This is an optional field for devices, but is required when
  1241. * performing synchronous ops */
  1242. prop = of_get_property(of_node, "ibm,resource-id", NULL);
  1243. if (prop != NULL)
  1244. viodev->resource_id = of_read_number(prop, 1);
  1245. dev_set_name(&viodev->dev, "%pOFn", of_node);
  1246. viodev->type = dev_name(&viodev->dev);
  1247. viodev->irq = 0;
  1248. }
  1249. viodev->name = of_node->name;
  1250. viodev->dev.of_node = of_node_get(of_node);
  1251. set_dev_node(&viodev->dev, of_node_to_nid(of_node));
  1252. /* init generic 'struct device' fields: */
  1253. viodev->dev.parent = &vio_bus_device.dev;
  1254. viodev->dev.bus = &vio_bus_type;
  1255. viodev->dev.release = vio_dev_release;
  1256. if (of_get_property(viodev->dev.of_node, "ibm,my-dma-window", NULL)) {
  1257. if (firmware_has_feature(FW_FEATURE_CMO))
  1258. vio_cmo_set_dma_ops(viodev);
  1259. else
  1260. set_dma_ops(&viodev->dev, &dma_iommu_ops);
  1261. set_iommu_table_base(&viodev->dev,
  1262. vio_build_iommu_table(viodev));
  1263. /* needed to ensure proper operation of coherent allocations
  1264. * later, in case driver doesn't set it explicitly */
  1265. viodev->dev.coherent_dma_mask = DMA_BIT_MASK(64);
  1266. viodev->dev.dma_mask = &viodev->dev.coherent_dma_mask;
  1267. }
  1268. /* register with generic device framework */
  1269. if (device_register(&viodev->dev)) {
  1270. printk(KERN_ERR "%s: failed to register device %s\n",
  1271. __func__, dev_name(&viodev->dev));
  1272. put_device(&viodev->dev);
  1273. return NULL;
  1274. }
  1275. return viodev;
  1276. out: /* Use this exit point for any return prior to device_register */
  1277. kfree(viodev);
  1278. return NULL;
  1279. }
  1280. EXPORT_SYMBOL(vio_register_device_node);
  1281. /*
  1282. * vio_bus_scan_for_devices - Scan OF and register each child device
  1283. * @root_name - OF node name for the root of the subtree to search.
  1284. * This must be non-NULL
  1285. *
  1286. * Starting from the root node provide, register the device node for
  1287. * each child beneath the root.
  1288. */
  1289. static void vio_bus_scan_register_devices(char *root_name)
  1290. {
  1291. struct device_node *node_root, *node_child;
  1292. if (!root_name)
  1293. return;
  1294. node_root = of_find_node_by_name(NULL, root_name);
  1295. if (node_root) {
  1296. /*
  1297. * Create struct vio_devices for each virtual device in
  1298. * the device tree. Drivers will associate with them later.
  1299. */
  1300. node_child = of_get_next_child(node_root, NULL);
  1301. while (node_child) {
  1302. vio_register_device_node(node_child);
  1303. node_child = of_get_next_child(node_root, node_child);
  1304. }
  1305. of_node_put(node_root);
  1306. }
  1307. }
  1308. /**
  1309. * vio_bus_init: - Initialize the virtual IO bus
  1310. */
  1311. static int __init vio_bus_init(void)
  1312. {
  1313. int err;
  1314. if (firmware_has_feature(FW_FEATURE_CMO))
  1315. vio_cmo_sysfs_init();
  1316. err = bus_register(&vio_bus_type);
  1317. if (err) {
  1318. printk(KERN_ERR "failed to register VIO bus\n");
  1319. return err;
  1320. }
  1321. /*
  1322. * The fake parent of all vio devices, just to give us
  1323. * a nice directory
  1324. */
  1325. err = device_register(&vio_bus_device.dev);
  1326. if (err) {
  1327. printk(KERN_WARNING "%s: device_register returned %i\n",
  1328. __func__, err);
  1329. return err;
  1330. }
  1331. if (firmware_has_feature(FW_FEATURE_CMO))
  1332. vio_cmo_bus_init();
  1333. return 0;
  1334. }
  1335. postcore_initcall(vio_bus_init);
  1336. static int __init vio_device_init(void)
  1337. {
  1338. vio_bus_scan_register_devices("vdevice");
  1339. vio_bus_scan_register_devices("ibm,platform-facilities");
  1340. return 0;
  1341. }
  1342. device_initcall(vio_device_init);
  1343. static ssize_t name_show(struct device *dev,
  1344. struct device_attribute *attr, char *buf)
  1345. {
  1346. return sprintf(buf, "%s\n", to_vio_dev(dev)->name);
  1347. }
  1348. static DEVICE_ATTR_RO(name);
  1349. static ssize_t devspec_show(struct device *dev,
  1350. struct device_attribute *attr, char *buf)
  1351. {
  1352. struct device_node *of_node = dev->of_node;
  1353. return sprintf(buf, "%pOF\n", of_node);
  1354. }
  1355. static DEVICE_ATTR_RO(devspec);
  1356. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  1357. char *buf)
  1358. {
  1359. const struct vio_dev *vio_dev = to_vio_dev(dev);
  1360. struct device_node *dn;
  1361. const char *cp;
  1362. dn = dev->of_node;
  1363. if (!dn) {
  1364. strcpy(buf, "\n");
  1365. return strlen(buf);
  1366. }
  1367. cp = of_get_property(dn, "compatible", NULL);
  1368. if (!cp) {
  1369. strcpy(buf, "\n");
  1370. return strlen(buf);
  1371. }
  1372. return sprintf(buf, "vio:T%sS%s\n", vio_dev->type, cp);
  1373. }
  1374. static DEVICE_ATTR_RO(modalias);
  1375. static struct attribute *vio_dev_attrs[] = {
  1376. &dev_attr_name.attr,
  1377. &dev_attr_devspec.attr,
  1378. &dev_attr_modalias.attr,
  1379. NULL,
  1380. };
  1381. ATTRIBUTE_GROUPS(vio_dev);
  1382. void vio_unregister_device(struct vio_dev *viodev)
  1383. {
  1384. device_unregister(&viodev->dev);
  1385. if (viodev->family == VDEVICE)
  1386. irq_dispose_mapping(viodev->irq);
  1387. }
  1388. EXPORT_SYMBOL(vio_unregister_device);
  1389. static int vio_bus_match(struct device *dev, struct device_driver *drv)
  1390. {
  1391. const struct vio_dev *vio_dev = to_vio_dev(dev);
  1392. struct vio_driver *vio_drv = to_vio_driver(drv);
  1393. const struct vio_device_id *ids = vio_drv->id_table;
  1394. return (ids != NULL) && (vio_match_device(ids, vio_dev) != NULL);
  1395. }
  1396. static int vio_hotplug(struct device *dev, struct kobj_uevent_env *env)
  1397. {
  1398. const struct vio_dev *vio_dev = to_vio_dev(dev);
  1399. struct device_node *dn;
  1400. const char *cp;
  1401. dn = dev->of_node;
  1402. if (!dn)
  1403. return -ENODEV;
  1404. cp = of_get_property(dn, "compatible", NULL);
  1405. if (!cp)
  1406. return -ENODEV;
  1407. add_uevent_var(env, "MODALIAS=vio:T%sS%s", vio_dev->type, cp);
  1408. return 0;
  1409. }
  1410. struct bus_type vio_bus_type = {
  1411. .name = "vio",
  1412. .dev_groups = vio_dev_groups,
  1413. .uevent = vio_hotplug,
  1414. .match = vio_bus_match,
  1415. .probe = vio_bus_probe,
  1416. .remove = vio_bus_remove,
  1417. };
  1418. /**
  1419. * vio_get_attribute: - get attribute for virtual device
  1420. * @vdev: The vio device to get property.
  1421. * @which: The property/attribute to be extracted.
  1422. * @length: Pointer to length of returned data size (unused if NULL).
  1423. *
  1424. * Calls prom.c's of_get_property() to return the value of the
  1425. * attribute specified by @which
  1426. */
  1427. const void *vio_get_attribute(struct vio_dev *vdev, char *which, int *length)
  1428. {
  1429. return of_get_property(vdev->dev.of_node, which, length);
  1430. }
  1431. EXPORT_SYMBOL(vio_get_attribute);
  1432. #ifdef CONFIG_PPC_PSERIES
  1433. /* vio_find_name() - internal because only vio.c knows how we formatted the
  1434. * kobject name
  1435. */
  1436. static struct vio_dev *vio_find_name(const char *name)
  1437. {
  1438. struct device *found;
  1439. found = bus_find_device_by_name(&vio_bus_type, NULL, name);
  1440. if (!found)
  1441. return NULL;
  1442. return to_vio_dev(found);
  1443. }
  1444. /**
  1445. * vio_find_node - find an already-registered vio_dev
  1446. * @vnode: device_node of the virtual device we're looking for
  1447. *
  1448. * Takes a reference to the embedded struct device which needs to be dropped
  1449. * after use.
  1450. */
  1451. struct vio_dev *vio_find_node(struct device_node *vnode)
  1452. {
  1453. char kobj_name[20];
  1454. struct device_node *vnode_parent;
  1455. const char *dev_type;
  1456. vnode_parent = of_get_parent(vnode);
  1457. if (!vnode_parent)
  1458. return NULL;
  1459. dev_type = of_get_property(vnode_parent, "device_type", NULL);
  1460. of_node_put(vnode_parent);
  1461. if (!dev_type)
  1462. return NULL;
  1463. /* construct the kobject name from the device node */
  1464. if (!strcmp(dev_type, "vdevice")) {
  1465. const __be32 *prop;
  1466. prop = of_get_property(vnode, "reg", NULL);
  1467. if (!prop)
  1468. return NULL;
  1469. snprintf(kobj_name, sizeof(kobj_name), "%x",
  1470. (uint32_t)of_read_number(prop, 1));
  1471. } else if (!strcmp(dev_type, "ibm,platform-facilities"))
  1472. snprintf(kobj_name, sizeof(kobj_name), "%pOFn", vnode);
  1473. else
  1474. return NULL;
  1475. return vio_find_name(kobj_name);
  1476. }
  1477. EXPORT_SYMBOL(vio_find_node);
  1478. int vio_enable_interrupts(struct vio_dev *dev)
  1479. {
  1480. int rc = h_vio_signal(dev->unit_address, VIO_IRQ_ENABLE);
  1481. if (rc != H_SUCCESS)
  1482. printk(KERN_ERR "vio: Error 0x%x enabling interrupts\n", rc);
  1483. return rc;
  1484. }
  1485. EXPORT_SYMBOL(vio_enable_interrupts);
  1486. int vio_disable_interrupts(struct vio_dev *dev)
  1487. {
  1488. int rc = h_vio_signal(dev->unit_address, VIO_IRQ_DISABLE);
  1489. if (rc != H_SUCCESS)
  1490. printk(KERN_ERR "vio: Error 0x%x disabling interrupts\n", rc);
  1491. return rc;
  1492. }
  1493. EXPORT_SYMBOL(vio_disable_interrupts);
  1494. #endif /* CONFIG_PPC_PSERIES */