saa7164-buffer.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "saa7164.h"
  22. /* The PCI address space for buffer handling looks like this:
  23. +-u32 wide-------------+
  24. | +
  25. +-u64 wide------------------------------------+
  26. + +
  27. +----------------------+
  28. | CurrentBufferPtr + Pointer to current PCI buffer >-+
  29. +----------------------+ |
  30. | Unused + |
  31. +----------------------+ |
  32. | Pitch + = 188 (bytes) |
  33. +----------------------+ |
  34. | PCI buffer size + = pitch * number of lines (312) |
  35. +----------------------+ |
  36. |0| Buf0 Write Offset + |
  37. +----------------------+ v
  38. |1| Buf1 Write Offset + |
  39. +----------------------+ |
  40. |2| Buf2 Write Offset + |
  41. +----------------------+ |
  42. |3| Buf3 Write Offset + |
  43. +----------------------+ |
  44. ... More write offsets |
  45. +---------------------------------------------+ |
  46. +0| set of ptrs to PCI pagetables + |
  47. +---------------------------------------------+ |
  48. +1| set of ptrs to PCI pagetables + <--------+
  49. +---------------------------------------------+
  50. +2| set of ptrs to PCI pagetables +
  51. +---------------------------------------------+
  52. +3| set of ptrs to PCI pagetables + >--+
  53. +---------------------------------------------+ |
  54. ... More buffer pointers | +----------------+
  55. +->| pt[0] TS data |
  56. | +----------------+
  57. |
  58. | +----------------+
  59. +->| pt[1] TS data |
  60. | +----------------+
  61. | etc
  62. */
  63. /* Allocate a new buffer structure and associated PCI space in bytes.
  64. * len must be a multiple of sizeof(u64)
  65. */
  66. struct saa7164_buffer *saa7164_buffer_alloc(struct saa7164_tsport *port,
  67. u32 len)
  68. {
  69. struct saa7164_buffer *buf = 0;
  70. struct saa7164_dev *dev = port->dev;
  71. int i;
  72. if ((len == 0) || (len >= 65536) || (len % sizeof(u64))) {
  73. log_warn("%s() SAA_ERR_BAD_PARAMETER\n", __func__);
  74. goto ret;
  75. }
  76. buf = kzalloc(sizeof(struct saa7164_buffer), GFP_KERNEL);
  77. if (buf == NULL) {
  78. log_warn("%s() SAA_ERR_NO_RESOURCES\n", __func__);
  79. goto ret;
  80. }
  81. buf->port = port;
  82. buf->flags = SAA7164_BUFFER_FREE;
  83. /* TODO: arg len is being ignored */
  84. buf->pci_size = SAA7164_PT_ENTRIES * 0x1000;
  85. buf->pt_size = (SAA7164_PT_ENTRIES * sizeof(u64)) + 0x1000;
  86. /* Allocate contiguous memory */
  87. buf->cpu = pci_alloc_consistent(port->dev->pci, buf->pci_size,
  88. &buf->dma);
  89. if (!buf->cpu)
  90. goto fail1;
  91. buf->pt_cpu = pci_alloc_consistent(port->dev->pci, buf->pt_size,
  92. &buf->pt_dma);
  93. if (!buf->pt_cpu)
  94. goto fail2;
  95. /* init the buffers to a known pattern, easier during debugging */
  96. memset(buf->cpu, 0xff, buf->pci_size);
  97. memset(buf->pt_cpu, 0xff, buf->pt_size);
  98. dprintk(DBGLVL_BUF, "%s() allocated buffer @ 0x%p\n", __func__, buf);
  99. dprintk(DBGLVL_BUF, " pci_cpu @ 0x%p dma @ 0x%08lx len = 0x%x\n",
  100. buf->cpu, (long)buf->dma, buf->pci_size);
  101. dprintk(DBGLVL_BUF, " pt_cpu @ 0x%p pt_dma @ 0x%08lx len = 0x%x\n",
  102. buf->pt_cpu, (long)buf->pt_dma, buf->pt_size);
  103. /* Format the Page Table Entries to point into the data buffer */
  104. for (i = 0 ; i < SAA7164_PT_ENTRIES; i++) {
  105. *(buf->pt_cpu + i) = buf->dma + (i * 0x1000); /* TODO */
  106. }
  107. goto ret;
  108. fail2:
  109. pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma);
  110. fail1:
  111. kfree(buf);
  112. buf = 0;
  113. ret:
  114. return buf;
  115. }
  116. int saa7164_buffer_dealloc(struct saa7164_tsport *port,
  117. struct saa7164_buffer *buf)
  118. {
  119. struct saa7164_dev *dev = port->dev;
  120. if ((buf == 0) || (port == 0))
  121. return SAA_ERR_BAD_PARAMETER;
  122. dprintk(DBGLVL_BUF, "%s() deallocating buffer @ 0x%p\n", __func__, buf);
  123. if (buf->flags != SAA7164_BUFFER_FREE)
  124. log_warn(" freeing a non-free buffer\n");
  125. pci_free_consistent(port->dev->pci, buf->pci_size, buf->cpu, buf->dma);
  126. pci_free_consistent(port->dev->pci, buf->pt_size, buf->pt_cpu,
  127. buf->pt_dma);
  128. kfree(buf);
  129. return SAA_OK;
  130. }