sth.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. iowrite8(*payload, sth->base + reg);
  87. break;
  88. case STP_PACKET_MERR:
  89. sth_iowrite(&out->MERR, payload, size);
  90. break;
  91. case STP_PACKET_FLAG:
  92. if (flags & STP_PACKET_TIMESTAMPED)
  93. outp = (u64 __iomem *)&out->FLAG_TS;
  94. else
  95. outp = (u64 __iomem *)&out->FLAG;
  96. size = 1;
  97. sth_iowrite(outp, payload, size);
  98. break;
  99. case STP_PACKET_USER:
  100. if (flags & STP_PACKET_TIMESTAMPED)
  101. outp = &out->USER_TS;
  102. else
  103. outp = &out->USER;
  104. sth_iowrite(outp, payload, size);
  105. break;
  106. case STP_PACKET_DATA:
  107. outp = &out->Dn;
  108. if (flags & STP_PACKET_TIMESTAMPED)
  109. outp += 2;
  110. if (flags & STP_PACKET_MARKED)
  111. outp++;
  112. sth_iowrite(outp, payload, size);
  113. break;
  114. }
  115. return size;
  116. }
  117. static phys_addr_t
  118. sth_stm_mmio_addr(struct stm_data *stm_data, unsigned int master,
  119. unsigned int channel, unsigned int nr_chans)
  120. {
  121. struct sth_device *sth = container_of(stm_data, struct sth_device, stm);
  122. phys_addr_t addr;
  123. master -= sth->stm.sw_start;
  124. addr = sth->channels_phys + (master * sth->stm.sw_nchannels + channel) *
  125. sizeof(struct intel_th_channel);
  126. if (offset_in_page(addr) ||
  127. offset_in_page(nr_chans * sizeof(struct intel_th_channel)))
  128. return 0;
  129. return addr;
  130. }
  131. static int sth_stm_link(struct stm_data *stm_data, unsigned int master,
  132. unsigned int channel)
  133. {
  134. struct sth_device *sth = container_of(stm_data, struct sth_device, stm);
  135. intel_th_set_output(to_intel_th_device(sth->dev), master);
  136. return 0;
  137. }
  138. static int intel_th_sw_init(struct sth_device *sth)
  139. {
  140. u32 reg;
  141. reg = ioread32(sth->base + REG_STH_STHCAP1);
  142. sth->stm.sw_nchannels = reg & 0xff;
  143. reg = ioread32(sth->base + REG_STH_STHCAP0);
  144. sth->stm.sw_start = reg & 0xffff;
  145. sth->stm.sw_end = reg >> 16;
  146. sth->sw_nmasters = sth->stm.sw_end - sth->stm.sw_start;
  147. dev_dbg(sth->dev, "sw_start: %x sw_end: %x masters: %x nchannels: %x\n",
  148. sth->stm.sw_start, sth->stm.sw_end, sth->sw_nmasters,
  149. sth->stm.sw_nchannels);
  150. return 0;
  151. }
  152. static int intel_th_sth_probe(struct intel_th_device *thdev)
  153. {
  154. struct device *dev = &thdev->dev;
  155. struct sth_device *sth;
  156. struct resource *res;
  157. void __iomem *base, *channels;
  158. int err;
  159. res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
  160. if (!res)
  161. return -ENODEV;
  162. base = devm_ioremap(dev, res->start, resource_size(res));
  163. if (!base)
  164. return -ENOMEM;
  165. res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 1);
  166. if (!res)
  167. return -ENODEV;
  168. channels = devm_ioremap(dev, res->start, resource_size(res));
  169. if (!channels)
  170. return -ENOMEM;
  171. sth = devm_kzalloc(dev, sizeof(*sth), GFP_KERNEL);
  172. if (!sth)
  173. return -ENOMEM;
  174. sth->dev = dev;
  175. sth->base = base;
  176. sth->channels = channels;
  177. sth->channels_phys = res->start;
  178. sth->stm.name = dev_name(dev);
  179. sth->stm.packet = sth_stm_packet;
  180. sth->stm.mmio_addr = sth_stm_mmio_addr;
  181. sth->stm.sw_mmiosz = sizeof(struct intel_th_channel);
  182. sth->stm.link = sth_stm_link;
  183. err = intel_th_sw_init(sth);
  184. if (err)
  185. return err;
  186. err = stm_register_device(dev, &sth->stm, THIS_MODULE);
  187. if (err) {
  188. dev_err(dev, "stm_register_device failed\n");
  189. return err;
  190. }
  191. dev_set_drvdata(dev, sth);
  192. return 0;
  193. }
  194. static void intel_th_sth_remove(struct intel_th_device *thdev)
  195. {
  196. struct sth_device *sth = dev_get_drvdata(&thdev->dev);
  197. stm_unregister_device(&sth->stm);
  198. }
  199. static struct intel_th_driver intel_th_sth_driver = {
  200. .probe = intel_th_sth_probe,
  201. .remove = intel_th_sth_remove,
  202. .driver = {
  203. .name = "sth",
  204. .owner = THIS_MODULE,
  205. },
  206. };
  207. module_driver(intel_th_sth_driver,
  208. intel_th_driver_register,
  209. intel_th_driver_unregister);
  210. MODULE_LICENSE("GPL v2");
  211. MODULE_DESCRIPTION("Intel(R) Trace Hub Software Trace Hub driver");
  212. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@intel.com>");