sth.c 5.7 KB

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