vuart.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  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. }
  420. memcpy(lb->data, buf, bytes);
  421. lb->head = lb->data;
  422. lb->tail = lb->data + bytes;
  423. lb->dbg_number = ++dbg_number;
  424. spin_lock_irqsave(&priv->tx_list.lock, flags);
  425. list_add_tail(&lb->link, &priv->tx_list.head);
  426. ps3_vuart_enable_interrupt_tx(dev);
  427. spin_unlock_irqrestore(&priv->tx_list.lock, flags);
  428. dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
  429. __func__, __LINE__, lb->dbg_number, bytes);
  430. return 0;
  431. }
  432. EXPORT_SYMBOL_GPL(ps3_vuart_write);
  433. /**
  434. * ps3_vuart_queue_rx_bytes - Queue waiting bytes into the buffer list.
  435. * @dev: The struct ps3_system_bus_device instance.
  436. * @bytes_queued: Number of bytes queued to the buffer list.
  437. *
  438. * Must be called with priv->rx_list.lock held.
  439. */
  440. static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device *dev,
  441. u64 *bytes_queued)
  442. {
  443. static unsigned long dbg_number;
  444. int result;
  445. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  446. struct list_buffer *lb;
  447. u64 bytes;
  448. *bytes_queued = 0;
  449. result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
  450. BUG_ON(result);
  451. if (result)
  452. return -EIO;
  453. if (!bytes)
  454. return 0;
  455. /* Add some extra space for recently arrived data. */
  456. bytes += 128;
  457. lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
  458. if (!lb)
  459. return -ENOMEM;
  460. ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
  461. lb->head = lb->data;
  462. lb->tail = lb->data + bytes;
  463. lb->dbg_number = ++dbg_number;
  464. list_add_tail(&lb->link, &priv->rx_list.head);
  465. priv->rx_list.bytes_held += bytes;
  466. dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %lxh bytes\n",
  467. __func__, __LINE__, lb->dbg_number, bytes);
  468. *bytes_queued = bytes;
  469. return 0;
  470. }
  471. /**
  472. * ps3_vuart_read - The entry point for reading data from a port.
  473. *
  474. * Queue data waiting at the port, and if enough bytes to satisfy the request
  475. * are held in the buffer list those bytes are dequeued and copied to the
  476. * caller's buffer. Emptied list buffers are retiered. If the request cannot
  477. * be statified by bytes held in the list buffers -EAGAIN is returned.
  478. */
  479. int ps3_vuart_read(struct ps3_system_bus_device *dev, void *buf,
  480. unsigned int bytes)
  481. {
  482. int result;
  483. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  484. unsigned long flags;
  485. struct list_buffer *lb, *n;
  486. unsigned long bytes_read;
  487. dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
  488. bytes, bytes);
  489. spin_lock_irqsave(&priv->rx_list.lock, flags);
  490. /* Queue rx bytes here for polled reads. */
  491. while (priv->rx_list.bytes_held < bytes) {
  492. u64 tmp;
  493. result = ps3_vuart_queue_rx_bytes(dev, &tmp);
  494. if (result || !tmp) {
  495. dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
  496. __func__, __LINE__,
  497. bytes - priv->rx_list.bytes_held);
  498. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  499. return -EAGAIN;
  500. }
  501. }
  502. list_for_each_entry_safe(lb, n, &priv->rx_list.head, link) {
  503. bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
  504. memcpy(buf, lb->head, bytes_read);
  505. buf += bytes_read;
  506. bytes -= bytes_read;
  507. priv->rx_list.bytes_held -= bytes_read;
  508. if (bytes_read < lb->tail - lb->head) {
  509. lb->head += bytes_read;
  510. dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
  511. "bytes\n", __func__, __LINE__, lb->dbg_number,
  512. bytes_read);
  513. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  514. return 0;
  515. }
  516. dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
  517. "bytes\n", __func__, __LINE__, lb->dbg_number,
  518. bytes_read);
  519. list_del(&lb->link);
  520. kfree(lb);
  521. }
  522. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  523. return 0;
  524. }
  525. EXPORT_SYMBOL_GPL(ps3_vuart_read);
  526. /**
  527. * ps3_vuart_work - Asynchronous read handler.
  528. */
  529. static void ps3_vuart_work(struct work_struct *work)
  530. {
  531. struct ps3_system_bus_device *dev =
  532. ps3_vuart_work_to_system_bus_dev(work);
  533. struct ps3_vuart_port_driver *drv =
  534. ps3_system_bus_dev_to_vuart_drv(dev);
  535. BUG_ON(!drv);
  536. drv->work(dev);
  537. }
  538. int ps3_vuart_read_async(struct ps3_system_bus_device *dev, unsigned int bytes)
  539. {
  540. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  541. unsigned long flags;
  542. if (priv->rx_list.work.trigger) {
  543. dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
  544. __func__, __LINE__);
  545. return -EAGAIN;
  546. }
  547. BUG_ON(!bytes);
  548. PREPARE_WORK(&priv->rx_list.work.work, ps3_vuart_work);
  549. spin_lock_irqsave(&priv->rx_list.lock, flags);
  550. if (priv->rx_list.bytes_held >= bytes) {
  551. dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
  552. __func__, __LINE__, bytes);
  553. schedule_work(&priv->rx_list.work.work);
  554. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  555. return 0;
  556. }
  557. priv->rx_list.work.trigger = bytes;
  558. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  559. dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
  560. __LINE__, bytes, bytes);
  561. return 0;
  562. }
  563. EXPORT_SYMBOL_GPL(ps3_vuart_read_async);
  564. void ps3_vuart_cancel_async(struct ps3_system_bus_device *dev)
  565. {
  566. to_port_priv(dev)->rx_list.work.trigger = 0;
  567. }
  568. EXPORT_SYMBOL_GPL(ps3_vuart_cancel_async);
  569. /**
  570. * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
  571. *
  572. * Services the transmit interrupt for the port. Writes as much data from the
  573. * buffer list as the port will accept. Retires any emptied list buffers and
  574. * adjusts the final list buffer state for a partial write.
  575. */
  576. static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device *dev)
  577. {
  578. int result = 0;
  579. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  580. unsigned long flags;
  581. struct list_buffer *lb, *n;
  582. unsigned long bytes_total = 0;
  583. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  584. spin_lock_irqsave(&priv->tx_list.lock, flags);
  585. list_for_each_entry_safe(lb, n, &priv->tx_list.head, link) {
  586. unsigned long bytes_written;
  587. result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
  588. &bytes_written);
  589. if (result) {
  590. dev_dbg(&dev->core,
  591. "%s:%d: ps3_vuart_raw_write failed\n",
  592. __func__, __LINE__);
  593. break;
  594. }
  595. bytes_total += bytes_written;
  596. if (bytes_written < lb->tail - lb->head) {
  597. lb->head += bytes_written;
  598. dev_dbg(&dev->core,
  599. "%s:%d cleared buf_%lu, %lxh bytes\n",
  600. __func__, __LINE__, lb->dbg_number,
  601. bytes_written);
  602. goto port_full;
  603. }
  604. dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
  605. lb->dbg_number);
  606. list_del(&lb->link);
  607. kfree(lb);
  608. }
  609. ps3_vuart_disable_interrupt_tx(dev);
  610. port_full:
  611. spin_unlock_irqrestore(&priv->tx_list.lock, flags);
  612. dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
  613. __func__, __LINE__, bytes_total);
  614. return result;
  615. }
  616. /**
  617. * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
  618. *
  619. * Services the receive interrupt for the port. Creates a list buffer and
  620. * copies all waiting port data to that buffer and enqueues the buffer in the
  621. * buffer list. Buffer list data is dequeued via ps3_vuart_read.
  622. */
  623. static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device *dev)
  624. {
  625. int result;
  626. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  627. unsigned long flags;
  628. u64 bytes;
  629. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  630. spin_lock_irqsave(&priv->rx_list.lock, flags);
  631. result = ps3_vuart_queue_rx_bytes(dev, &bytes);
  632. if (result) {
  633. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  634. return result;
  635. }
  636. if (priv->rx_list.work.trigger && priv->rx_list.bytes_held
  637. >= priv->rx_list.work.trigger) {
  638. dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
  639. __func__, __LINE__, priv->rx_list.work.trigger);
  640. priv->rx_list.work.trigger = 0;
  641. schedule_work(&priv->rx_list.work.work);
  642. }
  643. spin_unlock_irqrestore(&priv->rx_list.lock, flags);
  644. return result;
  645. }
  646. static int ps3_vuart_handle_interrupt_disconnect(
  647. struct ps3_system_bus_device *dev)
  648. {
  649. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  650. BUG_ON("no support");
  651. return -1;
  652. }
  653. /**
  654. * ps3_vuart_handle_port_interrupt - second stage interrupt handler
  655. *
  656. * Services any pending interrupt types for the port. Passes control to the
  657. * third stage type specific interrupt handler. Returns control to the first
  658. * stage handler after one iteration.
  659. */
  660. static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device *dev)
  661. {
  662. int result;
  663. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  664. unsigned long status;
  665. result = ps3_vuart_get_interrupt_status(dev, &status);
  666. if (result)
  667. return result;
  668. dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
  669. status);
  670. if (status & INTERRUPT_MASK_DISCONNECT) {
  671. priv->stats.disconnect_interrupts++;
  672. result = ps3_vuart_handle_interrupt_disconnect(dev);
  673. if (result)
  674. ps3_vuart_disable_interrupt_disconnect(dev);
  675. }
  676. if (status & INTERRUPT_MASK_TX) {
  677. priv->stats.tx_interrupts++;
  678. result = ps3_vuart_handle_interrupt_tx(dev);
  679. if (result)
  680. ps3_vuart_disable_interrupt_tx(dev);
  681. }
  682. if (status & INTERRUPT_MASK_RX) {
  683. priv->stats.rx_interrupts++;
  684. result = ps3_vuart_handle_interrupt_rx(dev);
  685. if (result)
  686. ps3_vuart_disable_interrupt_rx(dev);
  687. }
  688. return 0;
  689. }
  690. struct vuart_bus_priv {
  691. struct ports_bmp *bmp;
  692. unsigned int virq;
  693. struct semaphore probe_mutex;
  694. int use_count;
  695. struct ps3_system_bus_device *devices[PORT_COUNT];
  696. } static vuart_bus_priv;
  697. /**
  698. * ps3_vuart_irq_handler - first stage interrupt handler
  699. *
  700. * Loops finding any interrupting port and its associated instance data.
  701. * Passes control to the second stage port specific interrupt handler. Loops
  702. * until all outstanding interrupts are serviced.
  703. */
  704. static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
  705. {
  706. struct vuart_bus_priv *bus_priv = _private;
  707. BUG_ON(!bus_priv);
  708. while (1) {
  709. unsigned int port;
  710. dump_ports_bmp(bus_priv->bmp);
  711. port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status);
  712. if (port == BITS_PER_LONG)
  713. break;
  714. BUG_ON(port >= PORT_COUNT);
  715. BUG_ON(!bus_priv->devices[port]);
  716. ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
  717. }
  718. return IRQ_HANDLED;
  719. }
  720. static int ps3_vuart_bus_interrupt_get(void)
  721. {
  722. int result;
  723. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  724. vuart_bus_priv.use_count++;
  725. BUG_ON(vuart_bus_priv.use_count > 2);
  726. if (vuart_bus_priv.use_count != 1) {
  727. return 0;
  728. }
  729. BUG_ON(vuart_bus_priv.bmp);
  730. vuart_bus_priv.bmp = kzalloc(sizeof(struct ports_bmp), GFP_KERNEL);
  731. if (!vuart_bus_priv.bmp) {
  732. pr_debug("%s:%d: kzalloc failed.\n", __func__, __LINE__);
  733. result = -ENOMEM;
  734. goto fail_bmp_malloc;
  735. }
  736. result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY, vuart_bus_priv.bmp,
  737. &vuart_bus_priv.virq);
  738. if (result) {
  739. pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n",
  740. __func__, __LINE__, result);
  741. result = -EPERM;
  742. goto fail_alloc_irq;
  743. }
  744. result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
  745. IRQF_DISABLED, "vuart", &vuart_bus_priv);
  746. if (result) {
  747. pr_debug("%s:%d: request_irq failed (%d)\n",
  748. __func__, __LINE__, result);
  749. goto fail_request_irq;
  750. }
  751. pr_debug(" <- %s:%d: ok\n", __func__, __LINE__);
  752. return result;
  753. fail_request_irq:
  754. ps3_vuart_irq_destroy(vuart_bus_priv.virq);
  755. vuart_bus_priv.virq = NO_IRQ;
  756. fail_alloc_irq:
  757. kfree(vuart_bus_priv.bmp);
  758. vuart_bus_priv.bmp = NULL;
  759. fail_bmp_malloc:
  760. vuart_bus_priv.use_count--;
  761. pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
  762. return result;
  763. }
  764. static int ps3_vuart_bus_interrupt_put(void)
  765. {
  766. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  767. vuart_bus_priv.use_count--;
  768. BUG_ON(vuart_bus_priv.use_count < 0);
  769. if (vuart_bus_priv.use_count != 0)
  770. return 0;
  771. free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
  772. ps3_vuart_irq_destroy(vuart_bus_priv.virq);
  773. vuart_bus_priv.virq = NO_IRQ;
  774. kfree(vuart_bus_priv.bmp);
  775. vuart_bus_priv.bmp = NULL;
  776. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  777. return 0;
  778. }
  779. static int ps3_vuart_probe(struct ps3_system_bus_device *dev)
  780. {
  781. int result;
  782. struct ps3_vuart_port_driver *drv;
  783. struct ps3_vuart_port_priv *priv = NULL;
  784. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  785. drv = ps3_system_bus_dev_to_vuart_drv(dev);
  786. dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
  787. drv->core.core.name);
  788. BUG_ON(!drv);
  789. if (dev->port_number >= PORT_COUNT) {
  790. BUG();
  791. return -EINVAL;
  792. }
  793. down(&vuart_bus_priv.probe_mutex);
  794. result = ps3_vuart_bus_interrupt_get();
  795. if (result)
  796. goto fail_setup_interrupt;
  797. if (vuart_bus_priv.devices[dev->port_number]) {
  798. dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
  799. __LINE__, dev->port_number);
  800. result = -EBUSY;
  801. goto fail_busy;
  802. }
  803. vuart_bus_priv.devices[dev->port_number] = dev;
  804. /* Setup dev->driver_priv. */
  805. dev->driver_priv = kzalloc(sizeof(struct ps3_vuart_port_priv),
  806. GFP_KERNEL);
  807. if (!dev->driver_priv) {
  808. result = -ENOMEM;
  809. goto fail_dev_malloc;
  810. }
  811. priv = to_port_priv(dev);
  812. INIT_LIST_HEAD(&priv->tx_list.head);
  813. spin_lock_init(&priv->tx_list.lock);
  814. INIT_LIST_HEAD(&priv->rx_list.head);
  815. spin_lock_init(&priv->rx_list.lock);
  816. INIT_WORK(&priv->rx_list.work.work, NULL);
  817. priv->rx_list.work.trigger = 0;
  818. priv->rx_list.work.dev = dev;
  819. /* clear stale pending interrupts */
  820. ps3_vuart_clear_rx_bytes(dev, 0);
  821. ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
  822. ps3_vuart_set_triggers(dev, 1, 1);
  823. if (drv->probe)
  824. result = drv->probe(dev);
  825. else {
  826. result = 0;
  827. dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
  828. __LINE__);
  829. }
  830. if (result) {
  831. dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
  832. __func__, __LINE__);
  833. down(&vuart_bus_priv.probe_mutex);
  834. goto fail_probe;
  835. }
  836. up(&vuart_bus_priv.probe_mutex);
  837. return result;
  838. fail_probe:
  839. ps3_vuart_set_interrupt_mask(dev, 0);
  840. kfree(dev->driver_priv);
  841. dev->driver_priv = NULL;
  842. fail_dev_malloc:
  843. vuart_bus_priv.devices[dev->port_number] = NULL;
  844. fail_busy:
  845. ps3_vuart_bus_interrupt_put();
  846. fail_setup_interrupt:
  847. up(&vuart_bus_priv.probe_mutex);
  848. dev_dbg(&dev->core, "%s:%d: failed\n", __func__, __LINE__);
  849. return result;
  850. }
  851. /**
  852. * ps3_vuart_cleanup - common cleanup helper.
  853. * @dev: The struct ps3_system_bus_device instance.
  854. *
  855. * Cleans interrupts and HV resources. Must be called with
  856. * vuart_bus_priv.probe_mutex held. Used by ps3_vuart_remove and
  857. * ps3_vuart_shutdown. After this call, polled reading will still work.
  858. */
  859. static int ps3_vuart_cleanup(struct ps3_system_bus_device *dev)
  860. {
  861. dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
  862. ps3_vuart_cancel_async(dev);
  863. ps3_vuart_set_interrupt_mask(dev, 0);
  864. ps3_vuart_bus_interrupt_put();
  865. return 0;
  866. }
  867. /**
  868. * ps3_vuart_remove - Completely clean the device instance.
  869. * @dev: The struct ps3_system_bus_device instance.
  870. *
  871. * Cleans all memory, interrupts and HV resources. After this call the
  872. * device can no longer be used.
  873. */
  874. static int ps3_vuart_remove(struct ps3_system_bus_device *dev)
  875. {
  876. struct ps3_vuart_port_priv *priv = to_port_priv(dev);
  877. struct ps3_vuart_port_driver *drv;
  878. BUG_ON(!dev);
  879. down(&vuart_bus_priv.probe_mutex);
  880. dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
  881. dev->match_id);
  882. if (!dev->core.driver) {
  883. dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
  884. __LINE__);
  885. up(&vuart_bus_priv.probe_mutex);
  886. return 0;
  887. }
  888. drv = ps3_system_bus_dev_to_vuart_drv(dev);
  889. BUG_ON(!drv);
  890. if (drv->remove) {
  891. drv->remove(dev);
  892. } else {
  893. dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__,
  894. __LINE__);
  895. BUG();
  896. }
  897. ps3_vuart_cleanup(dev);
  898. vuart_bus_priv.devices[dev->port_number] = NULL;
  899. kfree(priv);
  900. priv = NULL;
  901. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  902. up(&vuart_bus_priv.probe_mutex);
  903. return 0;
  904. }
  905. /**
  906. * ps3_vuart_shutdown - Cleans interrupts and HV resources.
  907. * @dev: The struct ps3_system_bus_device instance.
  908. *
  909. * Cleans interrupts and HV resources. After this call the
  910. * device can still be used in polling mode. This behavior required
  911. * by sys-manager to be able to complete the device power operation
  912. * sequence.
  913. */
  914. static int ps3_vuart_shutdown(struct ps3_system_bus_device *dev)
  915. {
  916. struct ps3_vuart_port_driver *drv;
  917. BUG_ON(!dev);
  918. down(&vuart_bus_priv.probe_mutex);
  919. dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
  920. dev->match_id);
  921. if (!dev->core.driver) {
  922. dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
  923. __LINE__);
  924. up(&vuart_bus_priv.probe_mutex);
  925. return 0;
  926. }
  927. drv = ps3_system_bus_dev_to_vuart_drv(dev);
  928. BUG_ON(!drv);
  929. if (drv->shutdown)
  930. drv->shutdown(dev);
  931. else if (drv->remove) {
  932. dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n",
  933. __func__, __LINE__);
  934. drv->remove(dev);
  935. } else {
  936. dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__,
  937. __LINE__);
  938. BUG();
  939. }
  940. ps3_vuart_cleanup(dev);
  941. dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
  942. up(&vuart_bus_priv.probe_mutex);
  943. return 0;
  944. }
  945. static int __init ps3_vuart_bus_init(void)
  946. {
  947. pr_debug("%s:%d:\n", __func__, __LINE__);
  948. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  949. return -ENODEV;
  950. init_MUTEX(&vuart_bus_priv.probe_mutex);
  951. return 0;
  952. }
  953. static void __exit ps3_vuart_bus_exit(void)
  954. {
  955. pr_debug("%s:%d:\n", __func__, __LINE__);
  956. }
  957. core_initcall(ps3_vuart_bus_init);
  958. module_exit(ps3_vuart_bus_exit);
  959. /**
  960. * ps3_vuart_port_driver_register - Add a vuart port device driver.
  961. */
  962. int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
  963. {
  964. int result;
  965. pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
  966. BUG_ON(!drv->core.match_id);
  967. BUG_ON(!drv->core.core.name);
  968. drv->core.probe = ps3_vuart_probe;
  969. drv->core.remove = ps3_vuart_remove;
  970. drv->core.shutdown = ps3_vuart_shutdown;
  971. result = ps3_system_bus_driver_register(&drv->core);
  972. return result;
  973. }
  974. EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
  975. /**
  976. * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
  977. */
  978. void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
  979. {
  980. pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
  981. ps3_system_bus_driver_unregister(&drv->core);
  982. }
  983. EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);