ps3-vuart.c 31 KB

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