ps3-vuart.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PS3 virtual uart
  4. *
  5. * Copyright (C) 2006 Sony Computer Entertainment Inc.
  6. * Copyright 2006 Sony Corp.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/bitops.h>
  14. #include <asm/ps3.h>
  15. #include <asm/firmware.h>
  16. #include <asm/lv1call.h>
  17. #include "vuart.h"
  18. MODULE_AUTHOR("Sony Corporation");
  19. MODULE_LICENSE("GPL v2");
  20. MODULE_DESCRIPTION("PS3 vuart");
  21. /**
  22. * vuart - An inter-partition data link service.
  23. * port 0: PS3 AV Settings.
  24. * port 2: PS3 System Manager.
  25. *
  26. * The vuart provides a bi-directional byte stream data link between logical
  27. * partitions. Its primary role is as a communications link between the guest
  28. * OS and the system policy module. The current HV does not support any
  29. * connections other than those listed.
  30. */
  31. enum {PORT_COUNT = 3,};
  32. enum vuart_param {
  33. PARAM_TX_TRIGGER = 0,
  34. PARAM_RX_TRIGGER = 1,
  35. PARAM_INTERRUPT_MASK = 2,
  36. PARAM_RX_BUF_SIZE = 3, /* read only */
  37. PARAM_RX_BYTES = 4, /* read only */
  38. PARAM_TX_BUF_SIZE = 5, /* read only */
  39. PARAM_TX_BYTES = 6, /* read only */
  40. PARAM_INTERRUPT_STATUS = 7, /* read only */
  41. };
  42. enum vuart_interrupt_bit {
  43. INTERRUPT_BIT_TX = 0,
  44. INTERRUPT_BIT_RX = 1,
  45. INTERRUPT_BIT_DISCONNECT = 2,
  46. };
  47. enum vuart_interrupt_mask {
  48. INTERRUPT_MASK_TX = 1,
  49. INTERRUPT_MASK_RX = 2,
  50. INTERRUPT_MASK_DISCONNECT = 4,
  51. };
  52. /**
  53. * struct ps3_vuart_port_priv - private vuart device data.
  54. */
  55. struct ps3_vuart_port_priv {
  56. u64 interrupt_mask;
  57. struct {
  58. spinlock_t lock;
  59. struct list_head head;
  60. } tx_list;
  61. struct {
  62. struct ps3_vuart_work work;
  63. unsigned long bytes_held;
  64. spinlock_t lock;
  65. struct list_head head;
  66. } rx_list;
  67. struct ps3_vuart_stats stats;
  68. };
  69. static struct ps3_vuart_port_priv *to_port_priv(
  70. struct ps3_system_bus_device *dev)
  71. {
  72. BUG_ON(!dev);
  73. BUG_ON(!dev->driver_priv);
  74. return (struct ps3_vuart_port_priv *)dev->driver_priv;
  75. }
  76. /**
  77. * struct ports_bmp - bitmap indicating ports needing service.
  78. *
  79. * A 256 bit read only bitmap indicating ports needing service. Do not write
  80. * to these bits. Must not cross a page boundary.
  81. */
  82. struct ports_bmp {
  83. u64 status;
  84. u64 unused[3];
  85. } __attribute__((aligned(32)));
  86. #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
  87. static void __maybe_unused _dump_ports_bmp(
  88. const struct ports_bmp *bmp, const char *func, int line)
  89. {
  90. pr_debug("%s:%d: ports_bmp: %016llxh\n", func, line, bmp->status);
  91. }
  92. #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
  93. static void __maybe_unused _dump_port_params(unsigned int port_number,
  94. const char *func, int line)
  95. {
  96. #if defined(DEBUG)
  97. static const char *strings[] = {
  98. "tx_trigger ",
  99. "rx_trigger ",
  100. "interrupt_mask ",
  101. "rx_buf_size ",
  102. "rx_bytes ",
  103. "tx_buf_size ",
  104. "tx_bytes ",
  105. "interrupt_status",
  106. };
  107. int result;
  108. unsigned int i;
  109. u64 value;
  110. for (i = 0; i < ARRAY_SIZE(strings); i++) {
  111. result = lv1_get_virtual_uart_param(port_number, i, &value);
  112. if (result) {
  113. pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
  114. port_number, strings[i], ps3_result(result));
  115. continue;
  116. }
  117. pr_debug("%s:%d: port_%u: %s = %lxh\n",
  118. func, line, port_number, strings[i], value);
  119. }
  120. #endif
  121. }
  122. int ps3_vuart_get_triggers(struct ps3_system_bus_device *dev,
  123. struct vuart_triggers *trig)
  124. {
  125. int result;
  126. u64 size;
  127. u64 val;
  128. u64 tx;
  129. result = lv1_get_virtual_uart_param(dev->port_number,
  130. PARAM_TX_TRIGGER, &tx);
  131. trig->tx = tx;
  132. if (result) {
  133. dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
  134. __func__, __LINE__, ps3_result(result));
  135. return result;
  136. }
  137. result = lv1_get_virtual_uart_param(dev->port_number,
  138. PARAM_RX_BUF_SIZE, &size);
  139. if (result) {
  140. dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
  141. __func__, __LINE__, ps3_result(result));
  142. return result;
  143. }
  144. result = lv1_get_virtual_uart_param(dev->port_number,
  145. PARAM_RX_TRIGGER, &val);
  146. if (result) {
  147. dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
  148. __func__, __LINE__, ps3_result(result));
  149. return result;
  150. }
  151. trig->rx = size - val;
  152. dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
  153. trig->tx, trig->rx);
  154. return result;
  155. }
  156. int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx,
  157. unsigned int rx)
  158. {
  159. int result;
  160. u64 size;
  161. result = lv1_set_virtual_uart_param(dev->port_number,
  162. PARAM_TX_TRIGGER, tx);
  163. if (result) {
  164. dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
  165. __func__, __LINE__, ps3_result(result));
  166. return result;
  167. }
  168. result = lv1_get_virtual_uart_param(dev->port_number,
  169. PARAM_RX_BUF_SIZE, &size);
  170. if (result) {
  171. dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
  172. __func__, __LINE__, ps3_result(result));
  173. return result;
  174. }
  175. result = lv1_set_virtual_uart_param(dev->port_number,
  176. PARAM_RX_TRIGGER, size - rx);
  177. if (result) {
  178. dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
  179. __func__, __LINE__, ps3_result(result));
  180. return result;
  181. }
  182. dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
  183. tx, rx);
  184. return result;
  185. }
  186. static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev,
  187. u64 *bytes_waiting)
  188. {
  189. int result;
  190. result = lv1_get_virtual_uart_param(dev->port_number,
  191. PARAM_RX_BYTES, bytes_waiting);
  192. if (result)
  193. dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
  194. __func__, __LINE__, ps3_result(result));
  195. dev_dbg(&dev->core, "%s:%d: %llxh\n", __func__, __LINE__,
  196. *bytes_waiting);
  197. return result;
  198. }
  199. /**
  200. * ps3_vuart_set_interrupt_mask - Enable/disable the port interrupt sources.
  201. * @dev: The struct ps3_system_bus_device instance.
  202. * @bmp: Logical OR of enum vuart_interrupt_mask values. A zero bit disables.
  203. */
  204. static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device *dev,
  205. unsigned long mask)
  206. {
  207. int result;
  208. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  209. dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
  210. priv->interrupt_mask = mask;
  211. result = lv1_set_virtual_uart_param(dev->port_number,
  212. PARAM_INTERRUPT_MASK, priv->interrupt_mask);
  213. if (result)
  214. dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
  215. __func__, __LINE__, ps3_result(result));
  216. return result;
  217. }
  218. static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev,
  219. unsigned long *status)
  220. {
  221. int result;
  222. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  223. u64 tmp;
  224. result = lv1_get_virtual_uart_param(dev->port_number,
  225. PARAM_INTERRUPT_STATUS, &tmp);
  226. if (result)
  227. dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
  228. __func__, __LINE__, ps3_result(result));
  229. *status = tmp & priv->interrupt_mask;
  230. dev_dbg(&dev->core, "%s:%d: m %llxh, s %llxh, m&s %lxh\n",
  231. __func__, __LINE__, priv->interrupt_mask, tmp, *status);
  232. return result;
  233. }
  234. int ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device *dev)
  235. {
  236. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  237. return (priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
  238. : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
  239. | INTERRUPT_MASK_TX);
  240. }
  241. int ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device *dev)
  242. {
  243. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  244. return (priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
  245. : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
  246. | INTERRUPT_MASK_RX);
  247. }
  248. int ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device *dev)
  249. {
  250. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  251. return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
  252. : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
  253. | INTERRUPT_MASK_DISCONNECT);
  254. }
  255. int ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device *dev)
  256. {
  257. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  258. return (priv->interrupt_mask & INTERRUPT_MASK_TX)
  259. ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
  260. & ~INTERRUPT_MASK_TX) : 0;
  261. }
  262. int ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device *dev)
  263. {
  264. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  265. return (priv->interrupt_mask & INTERRUPT_MASK_RX)
  266. ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
  267. & ~INTERRUPT_MASK_RX) : 0;
  268. }
  269. int ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device *dev)
  270. {
  271. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  272. return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
  273. ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
  274. & ~INTERRUPT_MASK_DISCONNECT) : 0;
  275. }
  276. /**
  277. * ps3_vuart_raw_write - Low level write helper.
  278. * @dev: The struct ps3_system_bus_device instance.
  279. *
  280. * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
  281. */
  282. static int ps3_vuart_raw_write(struct ps3_system_bus_device *dev,
  283. const void *buf, unsigned int bytes, u64 *bytes_written)
  284. {
  285. int result;
  286. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  287. result = lv1_write_virtual_uart(dev->port_number,
  288. ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
  289. if (result) {
  290. dev_warn(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
  291. "%s\n", __func__, __LINE__, ps3_result(result));
  292. return result;
  293. }
  294. priv->stats.bytes_written += *bytes_written;
  295. dev_dbg(&dev->core, "%s:%d: wrote %llxh/%xh=>%lxh\n", __func__, __LINE__,
  296. *bytes_written, bytes, priv->stats.bytes_written);
  297. return result;
  298. }
  299. /**
  300. * ps3_vuart_raw_read - Low level read helper.
  301. * @dev: The struct ps3_system_bus_device instance.
  302. *
  303. * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
  304. */
  305. static int ps3_vuart_raw_read(struct ps3_system_bus_device *dev, void *buf,
  306. unsigned int bytes, u64 *bytes_read)
  307. {
  308. int result;
  309. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  310. dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
  311. result = lv1_read_virtual_uart(dev->port_number,
  312. ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
  313. if (result) {
  314. dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
  315. __func__, __LINE__, ps3_result(result));
  316. return result;
  317. }
  318. priv->stats.bytes_read += *bytes_read;
  319. dev_dbg(&dev->core, "%s:%d: read %llxh/%xh=>%lxh\n", __func__, __LINE__,
  320. *bytes_read, bytes, priv->stats.bytes_read);
  321. return result;
  322. }
  323. /**
  324. * ps3_vuart_clear_rx_bytes - Discard bytes received.
  325. * @dev: The struct ps3_system_bus_device instance.
  326. * @bytes: Max byte count to discard, zero = all pending.
  327. *
  328. * Used to clear pending rx interrupt source. Will not block.
  329. */
  330. void ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device *dev,
  331. unsigned int bytes)
  332. {
  333. int result;
  334. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  335. u64 bytes_waiting;
  336. void *tmp;
  337. result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
  338. BUG_ON(result);
  339. bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
  340. dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
  341. if (!bytes)
  342. return;
  343. /* Add some extra space for recently arrived data. */
  344. bytes += 128;
  345. tmp = kmalloc(bytes, GFP_KERNEL);
  346. if (!tmp)
  347. return;
  348. ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
  349. kfree(tmp);
  350. /* Don't include these bytes in the stats. */
  351. priv->stats.bytes_read -= bytes_waiting;
  352. }
  353. EXPORT_SYMBOL_GPL(ps3_vuart_clear_rx_bytes);
  354. /**
  355. * struct list_buffer - An element for a port device fifo buffer list.
  356. */
  357. struct list_buffer {
  358. struct list_head link;
  359. const unsigned char *head;
  360. const unsigned char *tail;
  361. unsigned long dbg_number;
  362. unsigned char data[];
  363. };
  364. /**
  365. * ps3_vuart_write - the entry point for writing data to a port
  366. * @dev: The struct ps3_system_bus_device instance.
  367. *
  368. * If the port is idle on entry as much of the incoming data is written to
  369. * the port as the port will accept. Otherwise a list buffer is created
  370. * and any remaining incoming data is copied to that buffer. The buffer is
  371. * then enqueued for transmission via the transmit interrupt.
  372. */
  373. int ps3_vuart_write(struct ps3_system_bus_device *dev, const void *buf,
  374. unsigned int bytes)
  375. {
  376. static unsigned long dbg_number;
  377. int result;
  378. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  379. unsigned long flags;
  380. struct list_buffer *lb;
  381. dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
  382. bytes, bytes);
  383. spin_lock_irqsave(&priv->tx_list.lock, flags);
  384. if (list_empty(&priv->tx_list.head)) {
  385. u64 bytes_written;
  386. result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
  387. spin_unlock_irqrestore(&priv->tx_list.lock, flags);
  388. if (result) {
  389. dev_dbg(&dev->core,
  390. "%s:%d: ps3_vuart_raw_write failed\n",
  391. __func__, __LINE__);
  392. return result;
  393. }
  394. if (bytes_written == bytes) {
  395. dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
  396. __func__, __LINE__, bytes);
  397. return 0;
  398. }
  399. bytes -= bytes_written;
  400. buf += bytes_written;
  401. } else
  402. spin_unlock_irqrestore(&priv->tx_list.lock, flags);
  403. lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
  404. if (!lb)
  405. return -ENOMEM;
  406. memcpy(lb->data, buf, bytes);
  407. lb->head = lb->data;
  408. lb->tail = lb->data + bytes;
  409. lb->dbg_number = ++dbg_number;
  410. spin_lock_irqsave(&priv->tx_list.lock, flags);
  411. list_add_tail(&lb->link, &priv->tx_list.head);
  412. ps3_vuart_enable_interrupt_tx(dev);
  413. spin_unlock_irqrestore(&priv->tx_list.lock, flags);
  414. dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
  415. __func__, __LINE__, lb->dbg_number, bytes);
  416. return 0;
  417. }
  418. EXPORT_SYMBOL_GPL(ps3_vuart_write);
  419. /**
  420. * ps3_vuart_queue_rx_bytes - Queue waiting bytes into the buffer list.
  421. * @dev: The struct ps3_system_bus_device instance.
  422. * @bytes_queued: Number of bytes queued to the buffer list.
  423. *
  424. * Must be called with priv->rx_list.lock held.
  425. */
  426. static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device *dev,
  427. u64 *bytes_queued)
  428. {
  429. static unsigned long dbg_number;
  430. int result;
  431. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  432. struct list_buffer *lb;
  433. u64 bytes;
  434. *bytes_queued = 0;
  435. result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
  436. BUG_ON(result);
  437. if (result)
  438. return -EIO;
  439. if (!bytes)
  440. return 0;
  441. /* Add some extra space for recently arrived data. */
  442. bytes += 128;
  443. lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
  444. if (!lb)
  445. return -ENOMEM;
  446. ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
  447. lb->head = lb->data;
  448. lb->tail = lb->data + bytes;
  449. lb->dbg_number = ++dbg_number;
  450. list_add_tail(&lb->link, &priv->rx_list.head);
  451. priv->rx_list.bytes_held += bytes;
  452. dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %llxh bytes\n",
  453. __func__, __LINE__, lb->dbg_number, bytes);
  454. *bytes_queued = bytes;
  455. return 0;
  456. }
  457. /**
  458. * ps3_vuart_read - The entry point for reading data from a port.
  459. *
  460. * Queue data waiting at the port, and if enough bytes to satisfy the request
  461. * are held in the buffer list those bytes are dequeued and copied to the
  462. * caller's buffer. Emptied list buffers are retiered. If the request cannot
  463. * be statified by bytes held in the list buffers -EAGAIN is returned.
  464. */
  465. int ps3_vuart_read(struct ps3_system_bus_device *dev, void *buf,
  466. unsigned int bytes)
  467. {
  468. int result;
  469. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  470. unsigned long flags;
  471. struct list_buffer *lb, *n;
  472. unsigned long bytes_read;
  473. dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
  474. bytes, bytes);
  475. spin_lock_irqsave(&priv->rx_list.lock, flags);
  476. /* Queue rx bytes here for polled reads. */
  477. while (priv->rx_list.bytes_held < bytes) {
  478. u64 tmp;
  479. result = ps3_vuart_queue_rx_bytes(dev, &tmp);
  480. if (result || !tmp) {
  481. dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
  482. __func__, __LINE__,
  483. bytes - priv->rx_list.bytes_held);
  484. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  485. return -EAGAIN;
  486. }
  487. }
  488. list_for_each_entry_safe(lb, n, &priv->rx_list.head, link) {
  489. bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
  490. memcpy(buf, lb->head, bytes_read);
  491. buf += bytes_read;
  492. bytes -= bytes_read;
  493. priv->rx_list.bytes_held -= bytes_read;
  494. if (bytes_read < lb->tail - lb->head) {
  495. lb->head += bytes_read;
  496. dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
  497. "bytes\n", __func__, __LINE__, lb->dbg_number,
  498. bytes_read);
  499. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  500. return 0;
  501. }
  502. dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
  503. "bytes\n", __func__, __LINE__, lb->dbg_number,
  504. bytes_read);
  505. list_del(&lb->link);
  506. kfree(lb);
  507. }
  508. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  509. return 0;
  510. }
  511. EXPORT_SYMBOL_GPL(ps3_vuart_read);
  512. /**
  513. * ps3_vuart_work - Asynchronous read handler.
  514. */
  515. static void ps3_vuart_work(struct work_struct *work)
  516. {
  517. struct ps3_system_bus_device *dev =
  518. ps3_vuart_work_to_system_bus_dev(work);
  519. struct ps3_vuart_port_driver *drv =
  520. ps3_system_bus_dev_to_vuart_drv(dev);
  521. BUG_ON(!drv);
  522. drv->work(dev);
  523. }
  524. int ps3_vuart_read_async(struct ps3_system_bus_device *dev, unsigned int bytes)
  525. {
  526. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  527. unsigned long flags;
  528. if (priv->rx_list.work.trigger) {
  529. dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
  530. __func__, __LINE__);
  531. return -EAGAIN;
  532. }
  533. BUG_ON(!bytes);
  534. spin_lock_irqsave(&priv->rx_list.lock, flags);
  535. if (priv->rx_list.bytes_held >= bytes) {
  536. dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
  537. __func__, __LINE__, bytes);
  538. schedule_work(&priv->rx_list.work.work);
  539. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  540. return 0;
  541. }
  542. priv->rx_list.work.trigger = bytes;
  543. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  544. dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
  545. __LINE__, bytes, bytes);
  546. return 0;
  547. }
  548. EXPORT_SYMBOL_GPL(ps3_vuart_read_async);
  549. void ps3_vuart_cancel_async(struct ps3_system_bus_device *dev)
  550. {
  551. to_port_priv(dev)->rx_list.work.trigger = 0;
  552. }
  553. EXPORT_SYMBOL_GPL(ps3_vuart_cancel_async);
  554. /**
  555. * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
  556. *
  557. * Services the transmit interrupt for the port. Writes as much data from the
  558. * buffer list as the port will accept. Retires any emptied list buffers and
  559. * adjusts the final list buffer state for a partial write.
  560. */
  561. static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device *dev)
  562. {
  563. int result = 0;
  564. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  565. unsigned long flags;
  566. struct list_buffer *lb, *n;
  567. unsigned long bytes_total = 0;
  568. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  569. spin_lock_irqsave(&priv->tx_list.lock, flags);
  570. list_for_each_entry_safe(lb, n, &priv->tx_list.head, link) {
  571. u64 bytes_written;
  572. result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
  573. &bytes_written);
  574. if (result) {
  575. dev_dbg(&dev->core,
  576. "%s:%d: ps3_vuart_raw_write failed\n",
  577. __func__, __LINE__);
  578. break;
  579. }
  580. bytes_total += bytes_written;
  581. if (bytes_written < lb->tail - lb->head) {
  582. lb->head += bytes_written;
  583. dev_dbg(&dev->core,
  584. "%s:%d cleared buf_%lu, %llxh bytes\n",
  585. __func__, __LINE__, lb->dbg_number,
  586. bytes_written);
  587. goto port_full;
  588. }
  589. dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
  590. lb->dbg_number);
  591. list_del(&lb->link);
  592. kfree(lb);
  593. }
  594. ps3_vuart_disable_interrupt_tx(dev);
  595. port_full:
  596. spin_unlock_irqrestore(&priv->tx_list.lock, flags);
  597. dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
  598. __func__, __LINE__, bytes_total);
  599. return result;
  600. }
  601. /**
  602. * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
  603. *
  604. * Services the receive interrupt for the port. Creates a list buffer and
  605. * copies all waiting port data to that buffer and enqueues the buffer in the
  606. * buffer list. Buffer list data is dequeued via ps3_vuart_read.
  607. */
  608. static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device *dev)
  609. {
  610. int result;
  611. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  612. unsigned long flags;
  613. u64 bytes;
  614. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  615. spin_lock_irqsave(&priv->rx_list.lock, flags);
  616. result = ps3_vuart_queue_rx_bytes(dev, &bytes);
  617. if (result) {
  618. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  619. return result;
  620. }
  621. if (priv->rx_list.work.trigger && priv->rx_list.bytes_held
  622. >= priv->rx_list.work.trigger) {
  623. dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
  624. __func__, __LINE__, priv->rx_list.work.trigger);
  625. priv->rx_list.work.trigger = 0;
  626. schedule_work(&priv->rx_list.work.work);
  627. }
  628. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  629. return result;
  630. }
  631. static int ps3_vuart_handle_interrupt_disconnect(
  632. struct ps3_system_bus_device *dev)
  633. {
  634. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  635. BUG_ON("no support");
  636. return -1;
  637. }
  638. /**
  639. * ps3_vuart_handle_port_interrupt - second stage interrupt handler
  640. *
  641. * Services any pending interrupt types for the port. Passes control to the
  642. * third stage type specific interrupt handler. Returns control to the first
  643. * stage handler after one iteration.
  644. */
  645. static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device *dev)
  646. {
  647. int result;
  648. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  649. unsigned long status;
  650. result = ps3_vuart_get_interrupt_status(dev, &status);
  651. if (result)
  652. return result;
  653. dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
  654. status);
  655. if (status & INTERRUPT_MASK_DISCONNECT) {
  656. priv->stats.disconnect_interrupts++;
  657. result = ps3_vuart_handle_interrupt_disconnect(dev);
  658. if (result)
  659. ps3_vuart_disable_interrupt_disconnect(dev);
  660. }
  661. if (status & INTERRUPT_MASK_TX) {
  662. priv->stats.tx_interrupts++;
  663. result = ps3_vuart_handle_interrupt_tx(dev);
  664. if (result)
  665. ps3_vuart_disable_interrupt_tx(dev);
  666. }
  667. if (status & INTERRUPT_MASK_RX) {
  668. priv->stats.rx_interrupts++;
  669. result = ps3_vuart_handle_interrupt_rx(dev);
  670. if (result)
  671. ps3_vuart_disable_interrupt_rx(dev);
  672. }
  673. return 0;
  674. }
  675. static struct vuart_bus_priv {
  676. struct ports_bmp *bmp;
  677. unsigned int virq;
  678. struct mutex probe_mutex;
  679. int use_count;
  680. struct ps3_system_bus_device *devices[PORT_COUNT];
  681. } vuart_bus_priv;
  682. /**
  683. * ps3_vuart_irq_handler - first stage interrupt handler
  684. *
  685. * Loops finding any interrupting port and its associated instance data.
  686. * Passes control to the second stage port specific interrupt handler. Loops
  687. * until all outstanding interrupts are serviced.
  688. */
  689. static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
  690. {
  691. struct vuart_bus_priv *bus_priv = _private;
  692. BUG_ON(!bus_priv);
  693. while (1) {
  694. unsigned int port;
  695. dump_ports_bmp(bus_priv->bmp);
  696. port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status);
  697. if (port == BITS_PER_LONG)
  698. break;
  699. BUG_ON(port >= PORT_COUNT);
  700. BUG_ON(!bus_priv->devices[port]);
  701. ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
  702. }
  703. return IRQ_HANDLED;
  704. }
  705. static int ps3_vuart_bus_interrupt_get(void)
  706. {
  707. int result;
  708. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  709. vuart_bus_priv.use_count++;
  710. BUG_ON(vuart_bus_priv.use_count > 2);
  711. if (vuart_bus_priv.use_count != 1)
  712. return 0;
  713. BUG_ON(vuart_bus_priv.bmp);
  714. vuart_bus_priv.bmp = kzalloc_obj(struct ports_bmp);
  715. if (!vuart_bus_priv.bmp) {
  716. result = -ENOMEM;
  717. goto fail_bmp_malloc;
  718. }
  719. result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY, vuart_bus_priv.bmp,
  720. &vuart_bus_priv.virq);
  721. if (result) {
  722. pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n",
  723. __func__, __LINE__, result);
  724. result = -EPERM;
  725. goto fail_alloc_irq;
  726. }
  727. result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
  728. 0, "vuart", &vuart_bus_priv);
  729. if (result) {
  730. pr_debug("%s:%d: request_irq failed (%d)\n",
  731. __func__, __LINE__, result);
  732. goto fail_request_irq;
  733. }
  734. pr_debug(" <- %s:%d: ok\n", __func__, __LINE__);
  735. return result;
  736. fail_request_irq:
  737. ps3_vuart_irq_destroy(vuart_bus_priv.virq);
  738. vuart_bus_priv.virq = 0;
  739. fail_alloc_irq:
  740. kfree(vuart_bus_priv.bmp);
  741. vuart_bus_priv.bmp = NULL;
  742. fail_bmp_malloc:
  743. vuart_bus_priv.use_count--;
  744. pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
  745. return result;
  746. }
  747. static int ps3_vuart_bus_interrupt_put(void)
  748. {
  749. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  750. vuart_bus_priv.use_count--;
  751. BUG_ON(vuart_bus_priv.use_count < 0);
  752. if (vuart_bus_priv.use_count != 0)
  753. return 0;
  754. free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
  755. ps3_vuart_irq_destroy(vuart_bus_priv.virq);
  756. vuart_bus_priv.virq = 0;
  757. kfree(vuart_bus_priv.bmp);
  758. vuart_bus_priv.bmp = NULL;
  759. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  760. return 0;
  761. }
  762. static int ps3_vuart_probe(struct ps3_system_bus_device *dev)
  763. {
  764. int result;
  765. struct ps3_vuart_port_driver *drv;
  766. struct ps3_vuart_port_priv *priv = NULL;
  767. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  768. drv = ps3_system_bus_dev_to_vuart_drv(dev);
  769. BUG_ON(!drv);
  770. dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
  771. drv->core.core.name);
  772. if (dev->port_number >= PORT_COUNT) {
  773. BUG();
  774. return -EINVAL;
  775. }
  776. mutex_lock(&vuart_bus_priv.probe_mutex);
  777. result = ps3_vuart_bus_interrupt_get();
  778. if (result)
  779. goto fail_setup_interrupt;
  780. if (vuart_bus_priv.devices[dev->port_number]) {
  781. dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
  782. __LINE__, dev->port_number);
  783. result = -EBUSY;
  784. goto fail_busy;
  785. }
  786. vuart_bus_priv.devices[dev->port_number] = dev;
  787. /* Setup dev->driver_priv. */
  788. dev->driver_priv = kzalloc_obj(struct ps3_vuart_port_priv);
  789. if (!dev->driver_priv) {
  790. result = -ENOMEM;
  791. goto fail_dev_malloc;
  792. }
  793. priv = to_port_priv(dev);
  794. INIT_LIST_HEAD(&priv->tx_list.head);
  795. spin_lock_init(&priv->tx_list.lock);
  796. INIT_LIST_HEAD(&priv->rx_list.head);
  797. spin_lock_init(&priv->rx_list.lock);
  798. INIT_WORK(&priv->rx_list.work.work, ps3_vuart_work);
  799. priv->rx_list.work.trigger = 0;
  800. priv->rx_list.work.dev = dev;
  801. /* clear stale pending interrupts */
  802. ps3_vuart_clear_rx_bytes(dev, 0);
  803. ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
  804. ps3_vuart_set_triggers(dev, 1, 1);
  805. if (drv->probe)
  806. result = drv->probe(dev);
  807. else {
  808. result = 0;
  809. dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
  810. __LINE__);
  811. }
  812. if (result) {
  813. dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
  814. __func__, __LINE__);
  815. goto fail_probe;
  816. }
  817. mutex_unlock(&vuart_bus_priv.probe_mutex);
  818. return result;
  819. fail_probe:
  820. ps3_vuart_set_interrupt_mask(dev, 0);
  821. kfree(dev->driver_priv);
  822. dev->driver_priv = NULL;
  823. fail_dev_malloc:
  824. vuart_bus_priv.devices[dev->port_number] = NULL;
  825. fail_busy:
  826. ps3_vuart_bus_interrupt_put();
  827. fail_setup_interrupt:
  828. mutex_unlock(&vuart_bus_priv.probe_mutex);
  829. dev_dbg(&dev->core, "%s:%d: failed\n", __func__, __LINE__);
  830. return result;
  831. }
  832. /**
  833. * ps3_vuart_cleanup - common cleanup helper.
  834. * @dev: The struct ps3_system_bus_device instance.
  835. *
  836. * Cleans interrupts and HV resources. Must be called with
  837. * vuart_bus_priv.probe_mutex held. Used by ps3_vuart_remove and
  838. * ps3_vuart_shutdown. After this call, polled reading will still work.
  839. */
  840. static int ps3_vuart_cleanup(struct ps3_system_bus_device *dev)
  841. {
  842. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  843. ps3_vuart_cancel_async(dev);
  844. ps3_vuart_set_interrupt_mask(dev, 0);
  845. ps3_vuart_bus_interrupt_put();
  846. return 0;
  847. }
  848. /**
  849. * ps3_vuart_remove - Completely clean the device instance.
  850. * @dev: The struct ps3_system_bus_device instance.
  851. *
  852. * Cleans all memory, interrupts and HV resources. After this call the
  853. * device can no longer be used.
  854. */
  855. static void ps3_vuart_remove(struct ps3_system_bus_device *dev)
  856. {
  857. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  858. struct ps3_vuart_port_driver *drv;
  859. BUG_ON(!dev);
  860. mutex_lock(&vuart_bus_priv.probe_mutex);
  861. dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
  862. dev->match_id);
  863. if (!dev->core.driver) {
  864. dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
  865. __LINE__);
  866. mutex_unlock(&vuart_bus_priv.probe_mutex);
  867. return;
  868. }
  869. drv = ps3_system_bus_dev_to_vuart_drv(dev);
  870. BUG_ON(!drv);
  871. if (drv->remove) {
  872. drv->remove(dev);
  873. } else {
  874. dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__,
  875. __LINE__);
  876. BUG();
  877. }
  878. ps3_vuart_cleanup(dev);
  879. vuart_bus_priv.devices[dev->port_number] = NULL;
  880. kfree(priv);
  881. priv = NULL;
  882. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  883. mutex_unlock(&vuart_bus_priv.probe_mutex);
  884. }
  885. /**
  886. * ps3_vuart_shutdown - Cleans interrupts and HV resources.
  887. * @dev: The struct ps3_system_bus_device instance.
  888. *
  889. * Cleans interrupts and HV resources. After this call the
  890. * device can still be used in polling mode. This behavior required
  891. * by sys-manager to be able to complete the device power operation
  892. * sequence.
  893. */
  894. static void ps3_vuart_shutdown(struct ps3_system_bus_device *dev)
  895. {
  896. struct ps3_vuart_port_driver *drv;
  897. BUG_ON(!dev);
  898. mutex_lock(&vuart_bus_priv.probe_mutex);
  899. dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
  900. dev->match_id);
  901. if (!dev->core.driver) {
  902. dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
  903. __LINE__);
  904. mutex_unlock(&vuart_bus_priv.probe_mutex);
  905. return;
  906. }
  907. drv = ps3_system_bus_dev_to_vuart_drv(dev);
  908. BUG_ON(!drv);
  909. if (drv->shutdown)
  910. drv->shutdown(dev);
  911. else if (drv->remove) {
  912. dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n",
  913. __func__, __LINE__);
  914. drv->remove(dev);
  915. } else {
  916. dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__,
  917. __LINE__);
  918. BUG();
  919. }
  920. ps3_vuart_cleanup(dev);
  921. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  922. mutex_unlock(&vuart_bus_priv.probe_mutex);
  923. }
  924. static int __init ps3_vuart_bus_init(void)
  925. {
  926. pr_debug("%s:%d:\n", __func__, __LINE__);
  927. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  928. return -ENODEV;
  929. mutex_init(&vuart_bus_priv.probe_mutex);
  930. return 0;
  931. }
  932. static void __exit ps3_vuart_bus_exit(void)
  933. {
  934. pr_debug("%s:%d:\n", __func__, __LINE__);
  935. }
  936. core_initcall(ps3_vuart_bus_init);
  937. module_exit(ps3_vuart_bus_exit);
  938. /**
  939. * ps3_vuart_port_driver_register - Add a vuart port device driver.
  940. */
  941. int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
  942. {
  943. int result;
  944. pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
  945. BUG_ON(!drv->core.match_id);
  946. BUG_ON(!drv->core.core.name);
  947. drv->core.probe = ps3_vuart_probe;
  948. drv->core.remove = ps3_vuart_remove;
  949. drv->core.shutdown = ps3_vuart_shutdown;
  950. result = ps3_system_bus_driver_register(&drv->core);
  951. return result;
  952. }
  953. EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
  954. /**
  955. * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
  956. */
  957. void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
  958. {
  959. pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
  960. ps3_system_bus_driver_unregister(&drv->core);
  961. }
  962. EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);