sth.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Intel(R) Trace Hub Software Trace Hub support
  3. *
  4. * Copyright (C) 2014-2015 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/types.h>
  17. #include <linux/module.h>
  18. #include <linux/device.h>
  19. #include <linux/io.h>
  20. #include <linux/mm.h>
  21. #include <linux/slab.h>
  22. #include <linux/stm.h>
  23. #include "intel_th.h"
  24. #include "sth.h"
  25. struct sth_device {
  26. void __iomem *base;
  27. void __iomem *channels;
  28. phys_addr_t channels_phys;
  29. struct device *dev;
  30. struct stm_data stm;
  31. unsigned int sw_nmasters;
  32. };
  33. static struct intel_th_channel __iomem *
  34. sth_channel(struct sth_device *sth, unsigned int master, unsigned int channel)
  35. {
  36. struct intel_th_channel __iomem *sw_map = sth->channels;
  37. return &sw_map[(master - sth->stm.sw_start) * sth->stm.sw_nchannels +
  38. channel];
  39. }
  40. static void sth_iowrite(void __iomem *dest, const unsigned char *payload,
  41. unsigned int size)
  42. {
  43. switch (size) {
  44. #ifdef CONFIG_64BIT
  45. case 8:
  46. writeq_relaxed(*(u64 *)payload, dest);
  47. break;
  48. #endif
  49. case 4:
  50. writel_relaxed(*(u32 *)payload, dest);
  51. break;
  52. case 2:
  53. writew_relaxed(*(u16 *)payload, dest);
  54. break;
  55. case 1:
  56. writeb_relaxed(*(u8 *)payload, dest);
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62. static ssize_t sth_stm_packet(struct stm_data *stm_data, unsigned int master,
  63. unsigned int channel, unsigned int packet,
  64. unsigned int flags, unsigned int size,
  65. const unsigned char *payload)
  66. {
  67. struct sth_device *sth = container_of(stm_data, struct sth_device, stm);
  68. struct intel_th_channel __iomem *out =
  69. sth_channel(sth, master, channel);
  70. u64 __iomem *outp = &out->Dn;
  71. unsigned long reg = REG_STH_TRIG;
  72. #ifndef CONFIG_64BIT
  73. if (size > 4)
  74. size = 4;
  75. #endif
  76. size = rounddown_pow_of_two(size);
  77. switch (packet) {
  78. /* Global packets (GERR, XSYNC, TRIG) are sent with register writes */
  79. case STP_PACKET_GERR:
  80. reg += 4;
  81. case STP_PACKET_XSYNC:
  82. reg += 8;
  83. case STP_PACKET_TRIG:
  84. if (flags & STP_PACKET_TIMESTAMPED)
  85. reg += 4;
  86. writeb_relaxed(*payload, sth->base + reg);
  87. break;
  88. case STP_PACKET_MERR:
  89. if (size > 4)
  90. size = 4;
  91. sth_iowrite(&out->MERR, payload, size);
  92. break;
  93. case STP_PACKET_FLAG:
  94. if (flags & STP_PACKET_TIMESTAMPED)
  95. outp = (u64 __iomem *)&out->FLAG_TS;
  96. else
  97. outp = (u64 __iomem *)&out->FLAG;
  98. size = 0;
  99. writeb_relaxed(0, outp);
  100. break;
  101. case STP_PACKET_USER:
  102. if (flags & STP_PACKET_TIMESTAMPED)
  103. outp = &out->USER_TS;
  104. else
  105. outp = &out->USER;
  106. sth_iowrite(outp, payload, size);
  107. break;
  108. case STP_PACKET_DATA:
  109. outp = &out->Dn;
  110. if (flags & STP_PACKET_TIMESTAMPED)
  111. outp += 2;
  112. if (flags & STP_PACKET_MARKED)
  113. outp++;
  114. sth_iowrite(outp, payload, size);
  115. break;
  116. default:
  117. return -ENOTSUPP;
  118. }
  119. return size;
  120. }
  121. static phys_addr_t
  122. sth_stm_mmio_addr(struct stm_data *stm_data, unsigned int master,
  123. unsigned int channel, unsigned int nr_chans)
  124. {
  125. struct sth_device *sth = container_of(stm_data, struct sth_device, stm);
  126. phys_addr_t addr;
  127. master -= sth->stm.sw_start;
  128. addr = sth->channels_phys + (master * sth->stm.sw_nchannels + channel) *
  129. sizeof(struct intel_th_channel);
  130. if (offset_in_page(addr) ||
  131. offset_in_page(nr_chans * sizeof(struct intel_th_channel)))
  132. return 0;
  133. return addr;
  134. }
  135. static int sth_stm_link(struct stm_data *stm_data, unsigned int master,
  136. unsigned int channel)
  137. {
  138. struct sth_device *sth = container_of(stm_data, struct sth_device, stm);
  139. intel_th_set_output(to_intel_th_device(sth->dev), master);
  140. return 0;
  141. }
  142. static int intel_th_sw_init(struct sth_device *sth)
  143. {
  144. u32 reg;
  145. reg = ioread32(sth->base + REG_STH_STHCAP1);
  146. sth->stm.sw_nchannels = reg & 0xff;
  147. reg = ioread32(sth->base + REG_STH_STHCAP0);
  148. sth->stm.sw_start = reg & 0xffff;
  149. sth->stm.sw_end = reg >> 16;
  150. sth->sw_nmasters = sth->stm.sw_end - sth->stm.sw_start;
  151. dev_dbg(sth->dev, "sw_start: %x sw_end: %x masters: %x nchannels: %x\n",
  152. sth->stm.sw_start, sth->stm.sw_end, sth->sw_nmasters,
  153. sth->stm.sw_nchannels);
  154. return 0;
  155. }
  156. static int intel_th_sth_probe(struct intel_th_device *thdev)
  157. {
  158. struct device *dev = &thdev->dev;
  159. struct sth_device *sth;
  160. struct resource *res;
  161. void __iomem *base, *channels;
  162. int err;
  163. res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
  164. if (!res)
  165. return -ENODEV;
  166. base = devm_ioremap(dev, res->start, resource_size(res));
  167. if (!base)
  168. return -ENOMEM;
  169. res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 1);
  170. if (!res)
  171. return -ENODEV;
  172. channels = devm_ioremap(dev, res->start, resource_size(res));
  173. if (!channels)
  174. return -ENOMEM;
  175. sth = devm_kzalloc(dev, sizeof(*sth), GFP_KERNEL);
  176. if (!sth)
  177. return -ENOMEM;
  178. sth->dev = dev;
  179. sth->base = base;
  180. sth->channels = channels;
  181. sth->channels_phys = res->start;
  182. sth->stm.name = dev_name(dev);
  183. sth->stm.packet = sth_stm_packet;
  184. sth->stm.mmio_addr = sth_stm_mmio_addr;
  185. sth->stm.sw_mmiosz = sizeof(struct intel_th_channel);
  186. sth->stm.link = sth_stm_link;
  187. err = intel_th_sw_init(sth);
  188. if (err)
  189. return err;
  190. err = stm_register_device(dev, &sth->stm, THIS_MODULE);
  191. if (err) {
  192. dev_err(dev, "stm_register_device failed\n");
  193. return err;
  194. }
  195. dev_set_drvdata(dev, sth);
  196. return 0;
  197. }
  198. static void intel_th_sth_remove(struct intel_th_device *thdev)
  199. {
  200. struct sth_device *sth = dev_get_drvdata(&thdev->dev);
  201. stm_unregister_device(&sth->stm);
  202. }
  203. static struct intel_th_driver intel_th_sth_driver = {
  204. .probe = intel_th_sth_probe,
  205. .remove = intel_th_sth_remove,
  206. .driver = {
  207. .name = "sth",
  208. .owner = THIS_MODULE,
  209. },
  210. };
  211. module_driver(intel_th_sth_driver,
  212. intel_th_driver_register,
  213. intel_th_driver_unregister);
  214. MODULE_LICENSE("GPL v2");
  215. MODULE_DESCRIPTION("Intel(R) Trace Hub Software Trace Hub driver");
  216. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@intel.com>");