ps3-vuart.c 31 KB

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