vuart.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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 <asm/ps3.h>
  25. #include <asm/firmware.h>
  26. #include <asm/lv1call.h>
  27. #include <asm/bitops.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 ports_bmp - bitmap indicating ports needing service.
  65. *
  66. * A 256 bit read only bitmap indicating ports needing service. Do not write
  67. * to these bits. Must not cross a page boundary.
  68. */
  69. struct ports_bmp {
  70. u64 status;
  71. u64 unused[3];
  72. } __attribute__ ((aligned (32)));
  73. /* redefine dev_dbg to do a syntax check */
  74. #if !defined(DEBUG)
  75. #undef dev_dbg
  76. static inline int __attribute__ ((format (printf, 2, 3))) dev_dbg(
  77. const struct device *_dev, const char *fmt, ...) {return 0;}
  78. #endif
  79. #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
  80. static void __attribute__ ((unused)) _dump_ports_bmp(
  81. const struct ports_bmp* bmp, const char* func, int line)
  82. {
  83. pr_debug("%s:%d: ports_bmp: %016lxh\n", func, line, bmp->status);
  84. }
  85. static int ps3_vuart_match_id_to_port(enum ps3_match_id match_id,
  86. unsigned int *port_number)
  87. {
  88. switch(match_id) {
  89. case PS3_MATCH_ID_AV_SETTINGS:
  90. *port_number = 0;
  91. return 0;
  92. case PS3_MATCH_ID_SYSTEM_MANAGER:
  93. *port_number = 2;
  94. return 0;
  95. default:
  96. WARN_ON(1);
  97. *port_number = UINT_MAX;
  98. return -EINVAL;
  99. };
  100. }
  101. #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
  102. static void __attribute__ ((unused)) _dump_port_params(unsigned int port_number,
  103. const char* func, int line)
  104. {
  105. #if defined(DEBUG)
  106. static const char *strings[] = {
  107. "tx_trigger ",
  108. "rx_trigger ",
  109. "interrupt_mask ",
  110. "rx_buf_size ",
  111. "rx_bytes ",
  112. "tx_buf_size ",
  113. "tx_bytes ",
  114. "interrupt_status",
  115. };
  116. int result;
  117. unsigned int i;
  118. u64 value;
  119. for (i = 0; i < ARRAY_SIZE(strings); i++) {
  120. result = lv1_get_virtual_uart_param(port_number, i, &value);
  121. if (result) {
  122. pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
  123. port_number, strings[i], ps3_result(result));
  124. continue;
  125. }
  126. pr_debug("%s:%d: port_%u: %s = %lxh\n",
  127. func, line, port_number, strings[i], value);
  128. }
  129. #endif
  130. }
  131. struct vuart_triggers {
  132. unsigned long rx;
  133. unsigned long tx;
  134. };
  135. int ps3_vuart_get_triggers(struct ps3_vuart_port_device *dev,
  136. struct vuart_triggers *trig)
  137. {
  138. int result;
  139. unsigned long size;
  140. unsigned long val;
  141. result = lv1_get_virtual_uart_param(dev->priv->port_number,
  142. PARAM_TX_TRIGGER, &trig->tx);
  143. if (result) {
  144. dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
  145. __func__, __LINE__, ps3_result(result));
  146. return result;
  147. }
  148. result = lv1_get_virtual_uart_param(dev->priv->port_number,
  149. PARAM_RX_BUF_SIZE, &size);
  150. if (result) {
  151. dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
  152. __func__, __LINE__, ps3_result(result));
  153. return result;
  154. }
  155. result = lv1_get_virtual_uart_param(dev->priv->port_number,
  156. PARAM_RX_TRIGGER, &val);
  157. if (result) {
  158. dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
  159. __func__, __LINE__, ps3_result(result));
  160. return result;
  161. }
  162. trig->rx = size - val;
  163. dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
  164. trig->tx, trig->rx);
  165. return result;
  166. }
  167. int ps3_vuart_set_triggers(struct ps3_vuart_port_device *dev, unsigned int tx,
  168. unsigned int rx)
  169. {
  170. int result;
  171. unsigned long size;
  172. result = lv1_set_virtual_uart_param(dev->priv->port_number,
  173. PARAM_TX_TRIGGER, tx);
  174. if (result) {
  175. dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
  176. __func__, __LINE__, ps3_result(result));
  177. return result;
  178. }
  179. result = lv1_get_virtual_uart_param(dev->priv->port_number,
  180. PARAM_RX_BUF_SIZE, &size);
  181. if (result) {
  182. dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
  183. __func__, __LINE__, ps3_result(result));
  184. return result;
  185. }
  186. result = lv1_set_virtual_uart_param(dev->priv->port_number,
  187. PARAM_RX_TRIGGER, size - rx);
  188. if (result) {
  189. dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
  190. __func__, __LINE__, ps3_result(result));
  191. return result;
  192. }
  193. dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
  194. tx, rx);
  195. return result;
  196. }
  197. static int ps3_vuart_get_rx_bytes_waiting(struct ps3_vuart_port_device *dev,
  198. u64 *bytes_waiting)
  199. {
  200. int result = lv1_get_virtual_uart_param(dev->priv->port_number,
  201. PARAM_RX_BYTES, bytes_waiting);
  202. if (result)
  203. dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
  204. __func__, __LINE__, ps3_result(result));
  205. dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__,
  206. *bytes_waiting);
  207. return result;
  208. }
  209. static int ps3_vuart_set_interrupt_mask(struct ps3_vuart_port_device *dev,
  210. unsigned long mask)
  211. {
  212. int result;
  213. dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
  214. dev->priv->interrupt_mask = mask;
  215. result = lv1_set_virtual_uart_param(dev->priv->port_number,
  216. PARAM_INTERRUPT_MASK, dev->priv->interrupt_mask);
  217. if (result)
  218. dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
  219. __func__, __LINE__, ps3_result(result));
  220. return result;
  221. }
  222. static int ps3_vuart_get_interrupt_status(struct ps3_vuart_port_device *dev,
  223. unsigned long *status)
  224. {
  225. u64 tmp;
  226. int result = lv1_get_virtual_uart_param(dev->priv->port_number,
  227. PARAM_INTERRUPT_STATUS, &tmp);
  228. if (result)
  229. dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
  230. __func__, __LINE__, ps3_result(result));
  231. *status = tmp & dev->priv->interrupt_mask;
  232. dev_dbg(&dev->core, "%s:%d: m %lxh, s %lxh, m&s %lxh\n",
  233. __func__, __LINE__, dev->priv->interrupt_mask, tmp, *status);
  234. return result;
  235. }
  236. int ps3_vuart_enable_interrupt_tx(struct ps3_vuart_port_device *dev)
  237. {
  238. return (dev->priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
  239. : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
  240. | INTERRUPT_MASK_TX);
  241. }
  242. int ps3_vuart_enable_interrupt_rx(struct ps3_vuart_port_device *dev)
  243. {
  244. return (dev->priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
  245. : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
  246. | INTERRUPT_MASK_RX);
  247. }
  248. int ps3_vuart_enable_interrupt_disconnect(struct ps3_vuart_port_device *dev)
  249. {
  250. return (dev->priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
  251. : ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
  252. | INTERRUPT_MASK_DISCONNECT);
  253. }
  254. int ps3_vuart_disable_interrupt_tx(struct ps3_vuart_port_device *dev)
  255. {
  256. return (dev->priv->interrupt_mask & INTERRUPT_MASK_TX)
  257. ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
  258. & ~INTERRUPT_MASK_TX) : 0;
  259. }
  260. int ps3_vuart_disable_interrupt_rx(struct ps3_vuart_port_device *dev)
  261. {
  262. return (dev->priv->interrupt_mask & INTERRUPT_MASK_RX)
  263. ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
  264. & ~INTERRUPT_MASK_RX) : 0;
  265. }
  266. int ps3_vuart_disable_interrupt_disconnect(struct ps3_vuart_port_device *dev)
  267. {
  268. return (dev->priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
  269. ? ps3_vuart_set_interrupt_mask(dev, dev->priv->interrupt_mask
  270. & ~INTERRUPT_MASK_DISCONNECT) : 0;
  271. }
  272. /**
  273. * ps3_vuart_raw_write - Low level write helper.
  274. *
  275. * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
  276. */
  277. static int ps3_vuart_raw_write(struct ps3_vuart_port_device *dev,
  278. const void* buf, unsigned int bytes, unsigned long *bytes_written)
  279. {
  280. int result;
  281. result = lv1_write_virtual_uart(dev->priv->port_number,
  282. ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
  283. if (result) {
  284. dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
  285. "%s\n", __func__, __LINE__, ps3_result(result));
  286. return result;
  287. }
  288. dev->priv->stats.bytes_written += *bytes_written;
  289. dev_dbg(&dev->core, "%s:%d: wrote %lxh/%xh=>%lxh\n", __func__, __LINE__,
  290. *bytes_written, bytes, dev->priv->stats.bytes_written);
  291. return result;
  292. }
  293. /**
  294. * ps3_vuart_raw_read - Low level read helper.
  295. *
  296. * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
  297. */
  298. static int ps3_vuart_raw_read(struct ps3_vuart_port_device *dev, void* buf,
  299. unsigned int bytes, unsigned long *bytes_read)
  300. {
  301. int result;
  302. dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
  303. result = lv1_read_virtual_uart(dev->priv->port_number,
  304. ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
  305. if (result) {
  306. dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
  307. __func__, __LINE__, ps3_result(result));
  308. return result;
  309. }
  310. dev->priv->stats.bytes_read += *bytes_read;
  311. dev_dbg(&dev->core, "%s:%d: read %lxh/%xh=>%lxh\n", __func__, __LINE__,
  312. *bytes_read, bytes, dev->priv->stats.bytes_read);
  313. return result;
  314. }
  315. /**
  316. * ps3_vuart_clear_rx_bytes - Discard bytes received.
  317. * @bytes: Max byte count to discard, zero = all pending.
  318. *
  319. * Used to clear pending rx interrupt source. Will not block.
  320. */
  321. void ps3_vuart_clear_rx_bytes(struct ps3_vuart_port_device *dev,
  322. unsigned int bytes)
  323. {
  324. int result;
  325. u64 bytes_waiting;
  326. void* tmp;
  327. result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
  328. BUG_ON(result);
  329. bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
  330. dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
  331. if (!bytes)
  332. return;
  333. /* Add some extra space for recently arrived data. */
  334. bytes += 128;
  335. tmp = kmalloc(bytes, GFP_KERNEL);
  336. if (!tmp)
  337. return;
  338. ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
  339. kfree(tmp);
  340. /* Don't include these bytes in the stats. */
  341. dev->priv->stats.bytes_read -= bytes_waiting;
  342. }
  343. /**
  344. * struct list_buffer - An element for a port device fifo buffer list.
  345. */
  346. struct list_buffer {
  347. struct list_head link;
  348. const unsigned char *head;
  349. const unsigned char *tail;
  350. unsigned long dbg_number;
  351. unsigned char data[];
  352. };
  353. /**
  354. * ps3_vuart_write - the entry point for writing data to a port
  355. *
  356. * If the port is idle on entry as much of the incoming data is written to
  357. * the port as the port will accept. Otherwise a list buffer is created
  358. * and any remaning incoming data is copied to that buffer. The buffer is
  359. * then enqueued for transmision via the transmit interrupt.
  360. */
  361. int ps3_vuart_write(struct ps3_vuart_port_device *dev, const void* buf,
  362. unsigned int bytes)
  363. {
  364. static unsigned long dbg_number;
  365. int result;
  366. unsigned long flags;
  367. struct list_buffer *lb;
  368. dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
  369. bytes, bytes);
  370. spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
  371. if (list_empty(&dev->priv->tx_list.head)) {
  372. unsigned long bytes_written;
  373. result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
  374. spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
  375. if (result) {
  376. dev_dbg(&dev->core,
  377. "%s:%d: ps3_vuart_raw_write failed\n",
  378. __func__, __LINE__);
  379. return result;
  380. }
  381. if (bytes_written == bytes) {
  382. dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
  383. __func__, __LINE__, bytes);
  384. return 0;
  385. }
  386. bytes -= bytes_written;
  387. buf += bytes_written;
  388. } else
  389. spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
  390. lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
  391. if (!lb) {
  392. return -ENOMEM;
  393. }
  394. memcpy(lb->data, buf, bytes);
  395. lb->head = lb->data;
  396. lb->tail = lb->data + bytes;
  397. lb->dbg_number = ++dbg_number;
  398. spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
  399. list_add_tail(&lb->link, &dev->priv->tx_list.head);
  400. ps3_vuart_enable_interrupt_tx(dev);
  401. spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
  402. dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
  403. __func__, __LINE__, lb->dbg_number, bytes);
  404. return 0;
  405. }
  406. /**
  407. * ps3_vuart_read - the entry point for reading data from a port
  408. *
  409. * If enough bytes to satisfy the request are held in the buffer list those
  410. * bytes are dequeued and copied to the caller's buffer. Emptied list buffers
  411. * are retiered. If the request cannot be statified by bytes held in the list
  412. * buffers -EAGAIN is returned.
  413. */
  414. int ps3_vuart_read(struct ps3_vuart_port_device *dev, void* buf,
  415. unsigned int bytes)
  416. {
  417. unsigned long flags;
  418. struct list_buffer *lb, *n;
  419. unsigned long bytes_read;
  420. dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
  421. bytes, bytes);
  422. spin_lock_irqsave(&dev->priv->rx_list.lock, flags);
  423. if (dev->priv->rx_list.bytes_held < bytes) {
  424. spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
  425. dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
  426. __func__, __LINE__,
  427. bytes - dev->priv->rx_list.bytes_held);
  428. return -EAGAIN;
  429. }
  430. list_for_each_entry_safe(lb, n, &dev->priv->rx_list.head, link) {
  431. bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
  432. memcpy(buf, lb->head, bytes_read);
  433. buf += bytes_read;
  434. bytes -= bytes_read;
  435. dev->priv->rx_list.bytes_held -= bytes_read;
  436. if (bytes_read < lb->tail - lb->head) {
  437. lb->head += bytes_read;
  438. dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
  439. "bytes\n", __func__, __LINE__, lb->dbg_number,
  440. bytes_read);
  441. spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
  442. return 0;
  443. }
  444. dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
  445. "bytes\n", __func__, __LINE__, lb->dbg_number,
  446. bytes_read);
  447. list_del(&lb->link);
  448. kfree(lb);
  449. }
  450. spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
  451. return 0;
  452. }
  453. int ps3_vuart_read_async(struct ps3_vuart_port_device *dev, work_func_t func,
  454. unsigned int bytes)
  455. {
  456. unsigned long flags;
  457. if(dev->priv->work.trigger) {
  458. dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
  459. __func__, __LINE__);
  460. return -EAGAIN;
  461. }
  462. BUG_ON(!bytes);
  463. PREPARE_WORK(&dev->priv->work.work, func);
  464. spin_lock_irqsave(&dev->priv->work.lock, flags);
  465. if(dev->priv->rx_list.bytes_held >= bytes) {
  466. dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
  467. __func__, __LINE__, bytes);
  468. schedule_work(&dev->priv->work.work);
  469. spin_unlock_irqrestore(&dev->priv->work.lock, flags);
  470. return 0;
  471. }
  472. dev->priv->work.trigger = bytes;
  473. spin_unlock_irqrestore(&dev->priv->work.lock, flags);
  474. dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
  475. __LINE__, bytes, bytes);
  476. return 0;
  477. }
  478. void ps3_vuart_cancel_async(struct ps3_vuart_port_device *dev)
  479. {
  480. dev->priv->work.trigger = 0;
  481. }
  482. /**
  483. * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
  484. *
  485. * Services the transmit interrupt for the port. Writes as much data from the
  486. * buffer list as the port will accept. Retires any emptied list buffers and
  487. * adjusts the final list buffer state for a partial write.
  488. */
  489. static int ps3_vuart_handle_interrupt_tx(struct ps3_vuart_port_device *dev)
  490. {
  491. int result = 0;
  492. unsigned long flags;
  493. struct list_buffer *lb, *n;
  494. unsigned long bytes_total = 0;
  495. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  496. spin_lock_irqsave(&dev->priv->tx_list.lock, flags);
  497. list_for_each_entry_safe(lb, n, &dev->priv->tx_list.head, link) {
  498. unsigned long bytes_written;
  499. result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
  500. &bytes_written);
  501. if (result) {
  502. dev_dbg(&dev->core,
  503. "%s:%d: ps3_vuart_raw_write failed\n",
  504. __func__, __LINE__);
  505. break;
  506. }
  507. bytes_total += bytes_written;
  508. if (bytes_written < lb->tail - lb->head) {
  509. lb->head += bytes_written;
  510. dev_dbg(&dev->core,
  511. "%s:%d cleared buf_%lu, %lxh bytes\n",
  512. __func__, __LINE__, lb->dbg_number,
  513. bytes_written);
  514. goto port_full;
  515. }
  516. dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
  517. lb->dbg_number);
  518. list_del(&lb->link);
  519. kfree(lb);
  520. }
  521. ps3_vuart_disable_interrupt_tx(dev);
  522. port_full:
  523. spin_unlock_irqrestore(&dev->priv->tx_list.lock, flags);
  524. dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
  525. __func__, __LINE__, bytes_total);
  526. return result;
  527. }
  528. /**
  529. * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
  530. *
  531. * Services the receive interrupt for the port. Creates a list buffer and
  532. * copies all waiting port data to that buffer and enqueues the buffer in the
  533. * buffer list. Buffer list data is dequeued via ps3_vuart_read.
  534. */
  535. static int ps3_vuart_handle_interrupt_rx(struct ps3_vuart_port_device *dev)
  536. {
  537. static unsigned long dbg_number;
  538. int result = 0;
  539. unsigned long flags;
  540. struct list_buffer *lb;
  541. unsigned long bytes;
  542. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  543. result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
  544. if (result)
  545. return -EIO;
  546. BUG_ON(!bytes);
  547. /* Add some extra space for recently arrived data. */
  548. bytes += 128;
  549. lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
  550. if (!lb)
  551. return -ENOMEM;
  552. ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
  553. lb->head = lb->data;
  554. lb->tail = lb->data + bytes;
  555. lb->dbg_number = ++dbg_number;
  556. spin_lock_irqsave(&dev->priv->rx_list.lock, flags);
  557. list_add_tail(&lb->link, &dev->priv->rx_list.head);
  558. dev->priv->rx_list.bytes_held += bytes;
  559. spin_unlock_irqrestore(&dev->priv->rx_list.lock, flags);
  560. dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %lxh bytes\n",
  561. __func__, __LINE__, lb->dbg_number, bytes);
  562. spin_lock_irqsave(&dev->priv->work.lock, flags);
  563. if(dev->priv->work.trigger
  564. && dev->priv->rx_list.bytes_held >= dev->priv->work.trigger) {
  565. dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
  566. __func__, __LINE__, dev->priv->work.trigger);
  567. dev->priv->work.trigger = 0;
  568. schedule_work(&dev->priv->work.work);
  569. }
  570. spin_unlock_irqrestore(&dev->priv->work.lock, flags);
  571. return 0;
  572. }
  573. static int ps3_vuart_handle_interrupt_disconnect(
  574. struct ps3_vuart_port_device *dev)
  575. {
  576. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  577. BUG_ON("no support");
  578. return -1;
  579. }
  580. /**
  581. * ps3_vuart_handle_port_interrupt - second stage interrupt handler
  582. *
  583. * Services any pending interrupt types for the port. Passes control to the
  584. * third stage type specific interrupt handler. Returns control to the first
  585. * stage handler after one iteration.
  586. */
  587. static int ps3_vuart_handle_port_interrupt(struct ps3_vuart_port_device *dev)
  588. {
  589. int result;
  590. unsigned long status;
  591. result = ps3_vuart_get_interrupt_status(dev, &status);
  592. if (result)
  593. return result;
  594. dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
  595. status);
  596. if (status & INTERRUPT_MASK_DISCONNECT) {
  597. dev->priv->stats.disconnect_interrupts++;
  598. result = ps3_vuart_handle_interrupt_disconnect(dev);
  599. if (result)
  600. ps3_vuart_disable_interrupt_disconnect(dev);
  601. }
  602. if (status & INTERRUPT_MASK_TX) {
  603. dev->priv->stats.tx_interrupts++;
  604. result = ps3_vuart_handle_interrupt_tx(dev);
  605. if (result)
  606. ps3_vuart_disable_interrupt_tx(dev);
  607. }
  608. if (status & INTERRUPT_MASK_RX) {
  609. dev->priv->stats.rx_interrupts++;
  610. result = ps3_vuart_handle_interrupt_rx(dev);
  611. if (result)
  612. ps3_vuart_disable_interrupt_rx(dev);
  613. }
  614. return 0;
  615. }
  616. struct vuart_bus_priv {
  617. const struct ports_bmp bmp;
  618. unsigned int virq;
  619. struct semaphore probe_mutex;
  620. int use_count;
  621. struct ps3_vuart_port_device *devices[PORT_COUNT];
  622. } static vuart_bus_priv;
  623. /**
  624. * ps3_vuart_irq_handler - first stage interrupt handler
  625. *
  626. * Loops finding any interrupting port and its associated instance data.
  627. * Passes control to the second stage port specific interrupt handler. Loops
  628. * until all outstanding interrupts are serviced.
  629. */
  630. static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
  631. {
  632. struct vuart_bus_priv *bus_priv;
  633. BUG_ON(!_private);
  634. bus_priv = (struct vuart_bus_priv *)_private;
  635. while (1) {
  636. unsigned int port;
  637. dump_ports_bmp(&bus_priv->bmp);
  638. port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp.status);
  639. if (port == BITS_PER_LONG)
  640. break;
  641. BUG_ON(port >= PORT_COUNT);
  642. BUG_ON(!bus_priv->devices[port]);
  643. ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
  644. }
  645. return IRQ_HANDLED;
  646. }
  647. static int ps3_vuart_match(struct device *_dev, struct device_driver *_drv)
  648. {
  649. int result;
  650. struct ps3_vuart_port_driver *drv = to_ps3_vuart_port_driver(_drv);
  651. struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
  652. result = dev->match_id == drv->match_id;
  653. dev_info(&dev->core, "%s:%d: dev=%u(%s), drv=%u(%s): %s\n", __func__,
  654. __LINE__, dev->match_id, dev->core.bus_id, drv->match_id,
  655. drv->core.name, (result ? "match" : "miss"));
  656. return result;
  657. }
  658. static int ps3_vuart_probe(struct device *_dev)
  659. {
  660. int result;
  661. unsigned int port_number;
  662. struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
  663. struct ps3_vuart_port_driver *drv =
  664. to_ps3_vuart_port_driver(_dev->driver);
  665. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  666. BUG_ON(!drv);
  667. down(&vuart_bus_priv.probe_mutex);
  668. /* Setup vuart_bus_priv.devices[]. */
  669. result = ps3_vuart_match_id_to_port(dev->match_id,
  670. &port_number);
  671. if (result) {
  672. dev_dbg(&dev->core, "%s:%d: unknown match_id (%d)\n",
  673. __func__, __LINE__, dev->match_id);
  674. result = -EINVAL;
  675. goto fail_match;
  676. }
  677. if (vuart_bus_priv.devices[port_number]) {
  678. dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
  679. __LINE__, port_number);
  680. result = -EBUSY;
  681. goto fail_match;
  682. }
  683. vuart_bus_priv.devices[port_number] = dev;
  684. /* Setup dev->priv. */
  685. dev->priv = kzalloc(sizeof(struct ps3_vuart_port_priv), GFP_KERNEL);
  686. if (!dev->priv) {
  687. result = -ENOMEM;
  688. goto fail_alloc;
  689. }
  690. dev->priv->port_number = port_number;
  691. INIT_LIST_HEAD(&dev->priv->tx_list.head);
  692. spin_lock_init(&dev->priv->tx_list.lock);
  693. INIT_LIST_HEAD(&dev->priv->rx_list.head);
  694. spin_lock_init(&dev->priv->rx_list.lock);
  695. INIT_WORK(&dev->priv->work.work, NULL);
  696. spin_lock_init(&dev->priv->work.lock);
  697. dev->priv->work.trigger = 0;
  698. dev->priv->work.dev = dev;
  699. if (++vuart_bus_priv.use_count == 1) {
  700. result = ps3_alloc_vuart_irq(PS3_BINDING_CPU_ANY,
  701. (void*)&vuart_bus_priv.bmp.status, &vuart_bus_priv.virq);
  702. if (result) {
  703. dev_dbg(&dev->core,
  704. "%s:%d: ps3_alloc_vuart_irq failed (%d)\n",
  705. __func__, __LINE__, result);
  706. result = -EPERM;
  707. goto fail_alloc_irq;
  708. }
  709. result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
  710. IRQF_DISABLED, "vuart", &vuart_bus_priv);
  711. if (result) {
  712. dev_info(&dev->core, "%s:%d: request_irq failed (%d)\n",
  713. __func__, __LINE__, result);
  714. goto fail_request_irq;
  715. }
  716. }
  717. /* clear stale pending interrupts */
  718. ps3_vuart_clear_rx_bytes(dev, 0);
  719. ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
  720. ps3_vuart_set_triggers(dev, 1, 1);
  721. if (drv->probe)
  722. result = drv->probe(dev);
  723. else {
  724. result = 0;
  725. dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
  726. __LINE__);
  727. }
  728. if (result) {
  729. dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
  730. __func__, __LINE__);
  731. down(&vuart_bus_priv.probe_mutex);
  732. goto fail_probe;
  733. }
  734. up(&vuart_bus_priv.probe_mutex);
  735. return result;
  736. fail_probe:
  737. ps3_vuart_set_interrupt_mask(dev, 0);
  738. fail_request_irq:
  739. ps3_free_vuart_irq(vuart_bus_priv.virq);
  740. vuart_bus_priv.virq = NO_IRQ;
  741. fail_alloc_irq:
  742. --vuart_bus_priv.use_count;
  743. kfree(dev->priv);
  744. dev->priv = NULL;
  745. fail_alloc:
  746. vuart_bus_priv.devices[port_number] = 0;
  747. fail_match:
  748. up(&vuart_bus_priv.probe_mutex);
  749. dev_dbg(&dev->core, "%s:%d failed\n", __func__, __LINE__);
  750. return result;
  751. }
  752. static int ps3_vuart_remove(struct device *_dev)
  753. {
  754. struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
  755. struct ps3_vuart_port_driver *drv =
  756. to_ps3_vuart_port_driver(_dev->driver);
  757. down(&vuart_bus_priv.probe_mutex);
  758. dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__,
  759. dev->core.bus_id);
  760. BUG_ON(vuart_bus_priv.use_count < 1);
  761. if (drv->remove)
  762. drv->remove(dev);
  763. else
  764. dev_dbg(&dev->core, "%s:%d: %s no remove method\n", __func__,
  765. __LINE__, dev->core.bus_id);
  766. vuart_bus_priv.devices[dev->priv->port_number] = 0;
  767. if (--vuart_bus_priv.use_count == 0) {
  768. BUG();
  769. free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
  770. ps3_free_vuart_irq(vuart_bus_priv.virq);
  771. vuart_bus_priv.virq = NO_IRQ;
  772. }
  773. kfree(dev->priv);
  774. dev->priv = NULL;
  775. up(&vuart_bus_priv.probe_mutex);
  776. return 0;
  777. }
  778. static void ps3_vuart_shutdown(struct device *_dev)
  779. {
  780. struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
  781. struct ps3_vuart_port_driver *drv =
  782. to_ps3_vuart_port_driver(_dev->driver);
  783. dev_dbg(&dev->core, "%s:%d: %s\n", __func__, __LINE__,
  784. dev->core.bus_id);
  785. if (drv->shutdown)
  786. drv->shutdown(dev);
  787. else
  788. dev_dbg(&dev->core, "%s:%d: %s no shutdown method\n", __func__,
  789. __LINE__, dev->core.bus_id);
  790. }
  791. /**
  792. * ps3_vuart_bus - The vuart bus instance.
  793. *
  794. * The vuart is managed as a bus that port devices connect to.
  795. */
  796. struct bus_type ps3_vuart_bus = {
  797. .name = "ps3_vuart",
  798. .match = ps3_vuart_match,
  799. .probe = ps3_vuart_probe,
  800. .remove = ps3_vuart_remove,
  801. .shutdown = ps3_vuart_shutdown,
  802. };
  803. int __init ps3_vuart_bus_init(void)
  804. {
  805. int result;
  806. pr_debug("%s:%d:\n", __func__, __LINE__);
  807. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  808. return 0;
  809. init_MUTEX(&vuart_bus_priv.probe_mutex);
  810. result = bus_register(&ps3_vuart_bus);
  811. BUG_ON(result);
  812. return result;
  813. }
  814. void __exit ps3_vuart_bus_exit(void)
  815. {
  816. pr_debug("%s:%d:\n", __func__, __LINE__);
  817. bus_unregister(&ps3_vuart_bus);
  818. }
  819. core_initcall(ps3_vuart_bus_init);
  820. module_exit(ps3_vuart_bus_exit);
  821. /**
  822. * ps3_vuart_port_release_device - Remove a vuart port device.
  823. */
  824. static void ps3_vuart_port_release_device(struct device *_dev)
  825. {
  826. #if defined(DEBUG)
  827. struct ps3_vuart_port_device *dev = to_ps3_vuart_port_device(_dev);
  828. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  829. BUG_ON(dev->priv && "forgot to free");
  830. memset(&dev->core, 0, sizeof(dev->core));
  831. #endif
  832. }
  833. /**
  834. * ps3_vuart_port_device_register - Add a vuart port device.
  835. */
  836. int ps3_vuart_port_device_register(struct ps3_vuart_port_device *dev)
  837. {
  838. static unsigned int dev_count = 1;
  839. BUG_ON(dev->priv && "forgot to free");
  840. dev->core.parent = NULL;
  841. dev->core.bus = &ps3_vuart_bus;
  842. dev->core.release = ps3_vuart_port_release_device;
  843. snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "vuart_%02x",
  844. dev_count++);
  845. dev_dbg(&dev->core, "%s:%d register\n", __func__, __LINE__);
  846. return device_register(&dev->core);
  847. }
  848. EXPORT_SYMBOL_GPL(ps3_vuart_port_device_register);
  849. /**
  850. * ps3_vuart_port_driver_register - Add a vuart port device driver.
  851. */
  852. int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
  853. {
  854. int result;
  855. pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name);
  856. drv->core.bus = &ps3_vuart_bus;
  857. result = driver_register(&drv->core);
  858. return result;
  859. }
  860. EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
  861. /**
  862. * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
  863. */
  864. void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
  865. {
  866. pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.name);
  867. driver_unregister(&drv->core);
  868. }
  869. EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);