#include "dma_packets.h" #define SPR_BEGIN 0x70000000 #define P_ALIGNMENT 64 #define P2_ALIGNMENT 64 #define P2_MAKE_PTR_NORMAL(PTR) ((uint32_t)(PTR)&0x0FFFFFFF) packet_t *packet_init( uint32_t qwords, uint16_t type){ uint32_t byte_size = 0; packet_t *packet = (packet_t*)malloc(sizeof(packet_t)); if( packet == NULL ) return NULL; if( type == PACKET_SPR ){ packet->data = (qword_t *) SPR_BEGIN; packet->qwc = 0x1000; } else { // Size of qwords in bytes. byte_size = qwords << 4; // Allocate the data area in bytes aligned to cache line. if(( packet->data = (qword_t *) memalign( P_ALIGNMENT, byte_size)) == NULL ){ free( packet ); return NULL; }; }; // Set the pointer attribute to ucab space. if( type == PACKET_UCAB ){ packet->data = (qword_t *)((uint32_t)packet->data | 0x30000000); }; // Clear the packet area. memset( packet->data, 0, byte_size ); // Set the packet counts packet->qwc = 0; packet->qwords = qwords; packet->type = type; // End function. return packet; }; void packet_free( packet_t *packet ){ if( packet == NULL ) return; // Free the allocated data buffer. if( packet->type == PACKET_SPR ){ packet->data = NULL; } else { if( packet->type == PACKET_UCAB ){ packet->data = (qword_t *)((uint32_t)packet->data ^ 0x30000000); }; free(packet->data); }; free(packet); }; void packet_reset( packet_t *packet ){ if( packet == NULL ) return; // Reset the quadword counter. packet->qwc = 0; if( packet->type == PACKET_SPR ){ packet->data = (qword_t *)SPR_BEGIN; return; }; // Zero out the data memset(packet->data, 0, packet->qwords << 4); }; // --- // Packet2 management // --- packet2_t* packet2_create(uint16_t qwords, packet2_type_t type, packet2_mode_t mode, uint8_t tte ){ packet2_t *packet2 = (packet2_t *)malloc(sizeof(packet2_t)); if( packet2 == NULL ) return NULL; memset( packet2, 0 , sizeof( packet2_t )); packet2->max_qwords_count = qwords; packet2->type = type; packet2->mode = mode; packet2->tte = tte; packet2->tag_opened_at = NULL; packet2->vif_code_opened_at = NULL; // Dma buffer size should be a whole number of cache lines (64 bytes = 4 quads) assert(!((packet2->type == P2_TYPE_UNCACHED || packet2->type == P2_TYPE_UNCACHED_ACCL) && packet2->max_qwords_count & (4 - 1))); uint32_t byte_size = packet2->max_qwords_count << 4; if((packet2->base = (qword_t *) memalign(P2_ALIGNMENT, byte_size)) == NULL){ free(packet2); return NULL; }; packet2->base = packet2->next = (qword_t *)((uint32_t)packet2->base | packet2->type); memset(packet2->base, 0, byte_size); // "I hate to do this, but I've wasted FAR too much time hunting down cache incoherency" if (packet2->type == P2_TYPE_UNCACHED || packet2->type == P2_TYPE_UNCACHED_ACCL) FlushCache(0); return packet2; }; packet2_t* packet2_create_from( qword_t *base, qword_t *next, uint16_t qwords, packet2_type_t type, packet2_mode_t mode, uint8_t tte ){ // Dma buffer size should be a whole number of cache lines (64 bytes = 4 quads) assert(!((type == P2_TYPE_UNCACHED || type == P2_TYPE_UNCACHED_ACCL) && qwords & (4 - 1))); // Dma buffer should be aligned on a cache line (64-byte boundary) assert(!((type == P2_TYPE_UNCACHED || type == P2_TYPE_UNCACHED_ACCL) && (uint32_t)base & (64 - 1))); packet2_t *packet2 = (packet2_t *) malloc(sizeof(packet2_t)); if (packet2 == NULL) return NULL; memset( packet2, 0 , sizeof( packet2_t )); packet2->base = base; packet2->next = next; packet2->max_qwords_count = qwords; packet2->type = type; packet2->tte = tte; packet2->mode = mode; return packet2; }; void packet2_free( packet2_t *packet2 ){ if( packet2 == NULL ) return; if( packet2->base != NULL ) free((qword_t *)P2_MAKE_PTR_NORMAL(packet2->base)); free( packet2 ); }; void packet2_reset( packet2_t *packet2, uint8_t clear_mem ){ if( packet2 == NULL ) return; packet2->next = packet2->base; packet2->vif_code_opened_at = NULL; packet2->tag_opened_at = NULL; if( clear_mem ) memset( packet2->base, 0, packet2->max_qwords_count << 4 ); }; // --- // Utils // --- void packet2_add( packet2_t *a, packet2_t *b ){ if( a== NULL || b == NULL ) return; assert(packet2_get_qw_count(a) + packet2_get_qw_count(b) <= a->max_qwords_count); memcpy(a->next, b->base, (uint32_t)b->next - (uint32_t)b->base); a->next = a->base + packet2_get_qw_count(b) + 1; }; void packet2_print( packet2_t *packet2, uint32_t qw_count){ if( packet2 == NULL ) return; if( qw_count == 0 ) qw_count = ((uint32_t)packet2->next - (uint32_t)packet2->base) >> 4; printf("\n============================\n"); printf("Packet2: Dumping %d words...\n", ((uint32_t)packet2->next - (uint32_t)packet2->base) >> 2); uint32_t i = 0; uint32_t * nextWord; for( nextWord = (uint32_t *)packet2->base; nextWord != (uint32_t *)packet2->next; nextWord++, i++ ){ if ((i % 4) == 0) printf("\n0x%08x: ", (uint32_t)nextWord ); printf( "0x%08x ", *nextWord ); if (i / 4 == qw_count) break; } printf("\n============================\n"); }; void packet2_print_qw_count( packet2_t *packet2 ){ if ( packet2 == NULL ) return; printf("Packet2: Qwords count:%d \n", packet2_get_qw_count(packet2)); }; void packet2_vif_add_micro_program( packet2_t *packet2, uint32_t dest, puint32_t start, puint32_t end ){ // get the size of the code as we can only send 256 instructions in each MPGtag uint32_t count = (end - start) >> 1; if( count & 1 ) count++; puint32_t l_start = start; while( count > 0 ){ uint16_t curr_count = ( count > 256 )? 256 : count; packet2_chain_ref( packet2, l_start, ( curr_count >> 1 ), 0, 0, 0); packet2_vif_nop( packet2, 0 ); packet2_vif_mpg( packet2, curr_count & 0xFF, dest, 0 ); l_start += ( curr_count << 1 ); count -= curr_count; dest += curr_count; }; }; uint32_t packet2_vif_close_unpack_auto( packet2_t *packet2, uint32_t wl, uint32_t cl ){ if( packet2 == NULL ) return 0; assert(packet2->vif_code_opened_at != NULL); // There is open UNPACK/DIRECT. assert(((uint32_t)packet2->next & 0x3) == 0); // Make sure we're u32 aligned assert((packet2->vif_code_opened_at->cmd & 0x60) == 0x60); // It was UNPACK uint32_t vn = (packet2->vif_code_opened_at->cmd & 0xC) >> 2; uint32_t vl = packet2->vif_code_opened_at->cmd & 0x3; // "the goal here is to find the num field of the open unpack, which is the number of // qwords ACTUALLY WRITTEN to vu memory (it does not count quads that are skipped in // "skipping write" mode.) But first forget about skipping/filling writes and compute the number // of qwords that the data in this packet will expand to." uint32_t bytes_count = (uint32_t)packet2->next - (uint32_t)packet2->vif_code_opened_at - 4; uint32_t block_bytes_count = 4 >> vl; uint32_t quad_blocks_count = vn + 1; // "make sure that the data length is a multiple of 8, 16, or 32 bits, whichever is appropriate" assert((bytes_count & (block_bytes_count - 1)) == 0); uint32_t quads_count = (bytes_count / block_bytes_count) / quad_blocks_count; // "We have the number of quads our data will directly expand to, so now we need to account for // skipping/filling write modes."" // "skipping write is easy -- we are already done" // "filling write: now we get ambiguous -- what to do when quads_count == CL in the last // block? I will say that in this case the vif should still do the full wl length block, filling // with internal registers. If you want different behavior call packet2_vif_close_unpack_manual // with a num field you've computed yourself" if( cl < wl ){ uint32_t wl_blocks_count = (quads_count / cl); uint32_t last_block_quads = quads_count - wl_blocks_count * cl; if (last_block_quads == cl) last_block_quads = wl; quads_count = wl_blocks_count * wl + last_block_quads; }; packet2_vif_close_unpack_manual( packet2, quads_count ); return quads_count; };