saa7164-buffer.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Driver for the NXP SAA7164 PCIe bridge
  3. *
  4. * Copyright (c) 2009 Steven Toth <stoth@kernellabs.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  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. *
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/slab.h>
  22. #include "saa7164.h"
  23. /* The PCI address space for buffer handling looks like this:
  24. +-u32 wide-------------+
  25. | +
  26. +-u64 wide------------------------------------+
  27. + +
  28. +----------------------+
  29. | CurrentBufferPtr + Pointer to current PCI buffer >-+
  30. +----------------------+ |
  31. | Unused + |
  32. +----------------------+ |
  33. | Pitch + = 188 (bytes) |
  34. +----------------------+ |
  35. | PCI buffer size + = pitch * number of lines (312) |
  36. +----------------------+ |
  37. |0| Buf0 Write Offset + |
  38. +----------------------+ v
  39. |1| Buf1 Write Offset + |
  40. +----------------------+ |
  41. |2| Buf2 Write Offset + |
  42. +----------------------+ |
  43. |3| Buf3 Write Offset + |
  44. +----------------------+ |
  45. ... More write offsets |
  46. +---------------------------------------------+ |
  47. +0| set of ptrs to PCI pagetables + |
  48. +---------------------------------------------+ |
  49. +1| set of ptrs to PCI pagetables + <--------+
  50. +---------------------------------------------+
  51. +2| set of ptrs to PCI pagetables +
  52. +---------------------------------------------+
  53. +3| set of ptrs to PCI pagetables + >--+
  54. +---------------------------------------------+ |
  55. ... More buffer pointers | +----------------+
  56. +->| pt[0] TS data |
  57. | +----------------+
  58. |
  59. | +----------------+
  60. +->| pt[1] TS data |
  61. | +----------------+
  62. | etc
  63. */
  64. /* Allocate a new buffer structure and associated PCI space in bytes.
  65. * len must be a multiple of sizeof(u64)
  66. */
  67. struct saa7164_buffer *saa7164_buffer_alloc(struct saa7164_tsport *port,
  68. u32 len)
  69. {
  70. struct saa7164_buffer *buf = 0;
  71. struct saa7164_dev *dev = port->dev;
  72. int i;
  73. if ((len == 0) || (len >= 65536) || (len % sizeof(u64))) {
  74. log_warn("%s() SAA_ERR_BAD_PARAMETER\n", __func__);
  75. goto ret;
  76. }
  77. buf = kzalloc(sizeof(struct saa7164_buffer), GFP_KERNEL);
  78. if (buf == NULL) {
  79. log_warn("%s() SAA_ERR_NO_RESOURCES\n", __func__);
  80. goto ret;
  81. }
  82. buf->port = port;
  83. buf->flags = SAA7164_BUFFER_FREE;
  84. /* TODO: arg len is being ignored */
  85. buf->pci_size = SAA7164_PT_ENTRIES * 0x1000;
  86. buf->pt_size = (SAA7164_PT_ENTRIES * sizeof(u64)) + 0x1000;
  87. /* Allocate contiguous memory */
  88. buf->cpu = pci_alloc_consistent(port->dev->pci, buf->pci_size,
  89. &buf->dma);
  90. if (!buf->cpu)
  91. goto fail1;
  92. buf->pt_cpu = pci_alloc_consistent(port->dev->pci, buf->pt_size,
  93. &buf->pt_dma);
  94. if (!buf->pt_cpu)
  95. goto fail2;
  96. /* init the buffers to a known pattern, easier during debugging */
  97. memset(buf->cpu, 0xff, buf->pci_size);
  98. memset(buf->pt_cpu, 0xff, buf->pt_size);
  99. dprintk(DBGLVL_BUF, "%s() allocated buffer @ 0x%p\n", __func__, buf);
  100. dprintk(DBGLVL_BUF, " pci_cpu @ 0x%p dma @ 0x%08lx len = 0x%x\n",
  101. buf->cpu, (long)buf->dma, buf->pci_size);
  102. dprintk(DBGLVL_BUF, " pt_cpu @ 0x%p pt_dma @ 0x%08lx len = 0x%x\n",
  103. buf->pt_cpu, (long)buf->pt_dma, buf->pt_size);
  104. /* Format the Page Table Entries to point into the data buffer */
  105. for (i = 0 ; i < SAA7164_PT_ENTRIES; i++) {
  106. *(buf->pt_cpu + i) = buf->dma + (i * 0x1000); /* TODO */
  107. }
  108. goto ret;
  109. fail2:
  110. pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma);
  111. fail1:
  112. kfree(buf);
  113. buf = 0;
  114. ret:
  115. return buf;
  116. }
  117. int saa7164_buffer_dealloc(struct saa7164_tsport *port,
  118. struct saa7164_buffer *buf)
  119. {
  120. struct saa7164_dev *dev;
  121. if (!buf || !port)
  122. return SAA_ERR_BAD_PARAMETER;
  123. dev = port->dev;
  124. dprintk(DBGLVL_BUF, "%s() deallocating buffer @ 0x%p\n", __func__, buf);
  125. if (buf->flags != SAA7164_BUFFER_FREE)
  126. log_warn(" freeing a non-free buffer\n");
  127. pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma);
  128. pci_free_consistent(port->dev->pci, buf->pt_size, buf->pt_cpu,
  129. buf->pt_dma);
  130. kfree(buf);
  131. return SAA_OK;
  132. }